Reputation: 1507
Today I encountered a terrible problem. I've been working on a whole framework with a class using variadic templates. And it turns out the guy I'm working with uses visual studio, which doesn't support variadic templates yet.
I tried to port the feature seamlessly by using macro, but after long work, it looks like it isn't possible as is. The problem is the quantity of parameter for the template may vary from zero to four (or to infinity with variadic templates).
Even though my macro are made, and expended in something like this :
template<>
Signal<void ()>
{
// My class
};
or
template<typename param1>
Signal<void (param1)>
{
// My class
};
But the problem is it looks like this doesn't work with gcc (so I'll assume it won't with vc as well).
The header doesn't show any errors, but if I try to instantiate the class like this :
Signal<> mySignal;
I get these kind of errors :
error: wrong number of template arguments (0, should be 1)
Since I find no information on the subject, I'd at least like to know if what I'm trying is possible ? Or if I'm really going to have to write again hundreds of lines of code because Microsoft doesn't think variadic templates are important enough ??
Upvotes: 0
Views: 606
Reputation: 208343
You cannot have a template with no arguments, the common approach to your problem is to use void
or any other placeholder. Note that since you are simulating a function-like object, you can probably simplify this by using a single type that is a function signature:
Signal< void() >
That way there is always a single template argument, and you just need to provide specializations of that template for the cases where the signature has 0 to 4 arguments:
template <typename S>
struct Signal;
template <typename arg1>
struct Signal<void (arg1)> { ... };
template <typename arg1, typename arg2>
struct Signal<void (arg1,arg2)> { ... };
If you are building a signal/handler or events library, I recommend that you take a look at existing implementations. Consider using or at least looking at the code in boost signals/signals2 library, for example.
The header doesn't show any errors, but if I try to instantiate the class like this :
Signal<> mySignal;
The problem is that your template takes a single argument, that is a function signature, but you are not providing any argument. At the point of instantiation you need to provide the template arguments. You probably mean to do:
Signal<void ()> mySignal;
Upvotes: 2