Answer by Gregory Currie for How can I dynamically pick a member from two...
This is a use case for type erasure. You basically want to make a section of the code not care about the actual type. (But still make sure that there is validation of the type elsewhere.)#include...
View ArticleAnswer by alfC for How can I dynamically pick a member from two similar C++...
This answer doesn't fulfill the last requirement of the question: "...I am unable to change the structures' layout in any way or form.".However I will leave it here as reference, since this is...
View ArticleAnswer by 康桓瑋 for How can I dynamically pick a member from two similar C++...
You can generate a function table at compile-time, then select the corresponding operation through the index at runtime:#include <array>struct XY { int test; };struct XYZ { short extra; int...
View ArticleAnswer by Yakk - Adam Nevraumont for How can I dynamically pick a member from...
Write a function that converts a void pointer to a variant of pointers.Then visit and dereference.You can write a magic member pointer that operates on a variant of pointers if you are addicted to...
View ArticleAnswer by rez for How can I dynamically pick a member from two similar C++...
#define FlexibleOffset(class1, class2, member) (x == 1 ? offsetof(class1, member) : offsetof(class2, member))#define FlexibleMember(object, member)...
View ArticleAnswer by Remy Lebeau for How can I dynamically pick a member from two...
Since XY::test and XYZ::test are at different byte offsets, and you have stated that you cannot change these structs, I think the best you can do is using an int* pointer, eg:int *ptest;switch (x) {...
View ArticleAnswer by Tony Delroy for How can I dynamically pick a member from two...
Well, since you explicitly say macros may be ok:#define CAST_IF(N, XY) if (x == N) reinterpret_cast<XY*>(object)#define ASSIGN(FIELD, VALUE) \ do { \ CAST_IF(1, XY)->FIELD = VALUE; \ else...
View ArticleHow can I dynamically pick a member from two similar C++ structures while...
Having the following structures,class XY{ int test;};class XYZ{ short extra; int test;};I want to use XY::test and XYZ::test depending on a runtime variable. Like:if (x == 1){...
View ArticleAnswer by user3840170 for How can I dynamically pick a member from two...
In C++20, you can use a generic lambda:#include <cstdlib>inline static auto dispatch(int x, void *ptr, auto perform) -> auto{ if (x == 1) return perform(reinterpret_cast<XY *>(ptr)); if...
View Article
More Pages to Explore .....