Reputation: 2888
I have a mixed container of fixed size that we'll call FixMix. I want to be able to invoke a constructor so that I don't need to write the type arguments out, since the compiler can infer what types are provided to the constructor. IE, instead of this:
FixMix<float, double> a(2.5f, 3.5);
I want to simplify it to this:
FixMix a(2.5f, 3.5);
Functions with variadic templates do this already, so I'm wondering if the constructor can so the same. Here's the basic class definition for FixMix:
template<typename... item_t> class FixMix {
public:
FixMix(void) {}
FixMix(item_t... items); //This is what I want to change
~FixMix(void);
};
There's not a lot of reading material on variadic templates, at least none that seem to answer this question, so I've just been trying to figure out the syntax on my own but to no avail. It's the ellipsis that's throwing me off, if it is even possible at all.
If it makes any difference I'm using Visual C++ 2013.
Upvotes: 1
Views: 143
Reputation: 961
I dont think you can do it in a constructor, but you could use a free function and auto to avoid repeating the types. Something like this should work:
template <typename... ARGS>
FixMix<ARGS...> make_FixMix(ARGS&&... args)
{
return FixMix<ARGS...>(std::forward<ARGS>(args)...);
}
Usage:
auto var=make_FixMix(2.5f,3.5);
Upvotes: 3