I would like to have a type, that would be in effect POD but I would like to be able to specify how and which types are in it, for example:
template<Args...>
struct POD
{
//here I would like to create depending on Args appropriate types as a members.
};
Is it possible to do it with this new variadic templates feature in C++0x?
I’ve never yet used the C++0x variadic templates feature but the following code compiles on G++ 4.5:
However, initializing them is … weird because we need to nest the inner values:
Retrieving the values is of course a bit tedious. The simplest method is probably to provide a
get<N>()function template.But we cannot implement
getdirectly since function templates cannot be partially specialized. Either we need to use SFINAE (read:boost::enable_if) or we need to delegate the actual function ofgetto a type that can be partially specialized.In the following, I did the latter. But first, we need another helper type trait:
nth_type, which returns the appropriate return type of thegetfunction:Easy-peasy. Just returns the nth type in a list of types.
Now we can write our
getfunction:Like I said, this just delegates the task. No biggie. In practice, we probably want to have another overload for
consttuples (but then, in practice we would use an existingtupletype).Now for the killing, followed by a light salad:
And that’s it. We can test this by printing some values in our previously defined variables:
Man, it’s fun messing with variadic templates.