views:

48

answers:

2

I'm using an C++ "event" class that allowed one or two arguments in the to be called delegates.

Lately I've added support for delegates that don't require arguments, however when I specialze the class to use no template arguments I'm still required to add <> afther the class definition.

Example usage with one/two arguments:

class Example 
{
public:
    event<int> SingleArgEvent;
    event<int, int> DoubleArgEvent;
};

And here the no argument example:

class Example 
{
public:
    event<> NoArgTest1; // compiles just fine
    event NoArgTest2;   // gives me C2955: 'event' : use of class template requires template argument list
};

As it works with the brackets, I don't see a reason why they should to be there. Here's the class specialization code:

class null_typelist {};
/** event template **/
template <class Targ1 = null_typelist, class Targ2 = null_typelist>
class event : public event2_base<Targ1, Targ2>
{
};

template <class Targ>
class event<Targ, null_typelist> : public event1_base<Targ>
{
};

template <>
class event<null_typelist, null_typelist> : public event0_base
{
};
+3  A: 

If you want to call a function foo with no arguments you have to write foo() not just foo. That is exactly the same for template types. event is not a type, it is a "function" that takes types and return a type. If you really want to avoid writing the brackets, you can still do something like:

typedef event<> event_;
Ugo
The Idea was to have the same class name "event" no matter how much template arguments. With an typedef that goes away. I guess there's no workaround?
Stormenet
+3  A: 

Brackets are required because the C++ grammar says so.

In 14.2/1 (temp.names/1), < and > are not optional, while template-argument-list is. So event<> is a valid template-id while event is not due to lack of <>.

template-id:
           template-name  < template-argument-list opt >
czchen