I have structs templated by int derived from a Base struct.
struct Base { int i; double d; };
template< int N > struct Derv : base { static const int mN = N; };
I need to make an array of Derv< N > where N can vary for each struct in that array. I know C/C++ does not allow arrays of objects of different types, but is there a way around this? I was thinking of separating the type information somehow (hints like pointers to Base struct or usage of union spring to my mind, but with all of these I don’t know how to store the type information of each array element for usage DURING COMPILE TIME). As you can see, the memory pattern of each Derv< N > is the same.
I need to access the type of each array element for template specialization later in my code. The general aim of this all is to have a compile-time dispatch mechanism without the need to do a runtime “type switch” somewhere in the code.
It is most certainly impossible. If you did
Then what is the type of
X[i]? Oh, wait, you can’t possibly know. It’s nothing C++ specific, it’s fundamentally impossible. That’s why nosome_magic_arraywill ever exist that permits this.Unless you effectively use a
std::tupleand guarantee thatiis constexpr. Then you absolutely can do this withstd::get<i>(tup);.