Like this:
typeArray type[2] = {uint8_t, uint16_t};
for (int i=0; i < 2; i++)
{
myFunction<type[i]>();
}
I think it is impossible to implementation, because it break some rules in C++. But I'm not sure, and I'm not familiar with the new features of C++(11 14 17 etc.). so could anyone tell me the answer?
More specific.
\\ Such as I have a template functon "PrintSecondTypeLen"
template<typename Type>PrintSecondTypeLen(unsigned int *a)
{
Type *b;
b = (Type *) a;
std::cout << *(b+1) << std::endl;
}
\\ Then suppose that I have a typeArray.
typeArray type[2] = {uint8_t, uint16_t}; \\ It's wrong, just for example.
\\ Then I use it:
unsigned int a[2] = {0x56575859, 0x46474849};
for (int i=0; i < 2; i++)
{
PrintSecondTypeLen<type[i]>(a);
}
In C++ types are not first-class values at runtime. You can have type lists at compile time for template metaprogramming but not a run time.
Your "loop" could be implemented with a variadic template but expanding it at compile time, not at run time.
May be (may be) types as first-class values is something will be introduced with reflection in C++23. In which form and what will you be able to do with it is another issue.
The main problem for C++ on this is that types do no actually exist in a compiled program. Types in a sense are part of the code and a compiled program doesn't deal with code but with data (in C++ data is not code and code is not data; this "barrier" will not be easy to break also for psychological reasons).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With