Reputation: 966
I am new to C++11's variadic templates, and do not know if what I am doing wrong is a simple syntax error, or is impossible. I would like to write something like the following:
template< typename... Args >
struct Test
{
Args... args;
Test( Args... args_ ) : args( args_... ) {
}
};
Basically this class would have "pseudo dynamic members". I have tried this with:
struct A {};
template< typename... Args >
A* MakeB( Args... args )
{
struct B : public A
{
Args... args;
B( Args... args_ ) : args( args_... ) {
}
};
return new B;
};
Is it impossible, or am I just not good with variadic templates?
Upvotes: 0
Views: 268
Reputation: 477348
Packs are not types, and cannot be used like types. They are a special construction only for use in templates. You cannot have a "pack member".
The typical solution is to have a tuple member:
#include <tuple>
template< typename... Args >
struct Test
{
std::tuple<Args...> args_;
Test(Args const &... args)
: args_(args_...)
{ }
};
Your little maker would look like this:
template <typename ...Args>
Test<Args...> make_test<Args const &... args)
{
return Test<Args>(args...);
}
Usage:
auto t = make_test(1, true, 'x'); // makes a Test<int, bool, char>
Upvotes: 4