Array of templated structs

Posted by Jakub Mertlik on Stack Overflow See other posts from Stack Overflow or by Jakub Mertlik
Published on 2012-10-07T15:34:23Z Indexed on 2012/10/07 15:37 UTC
Read the original article Hit count: 266

Filed under:
|
|

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.

Thank you.

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates