Type of function in the template does not compile

I played with the parameters of the variation template using gcc 4.6.1. The following code compiles as expected:

template<typename RetType, typename... ArgTypes>
class Event;

template<typename RetType, typename... ArgTypes>
class Event<RetType(ArgTypes...)>
{
public:
    typedef function<RetType(ArgTypes...)> CallbackType;

    void emit(ArgTypes...args)
    {
        for (CallbackType callback : callbacks)
        {
            callback(args...);
        }
    }

private:
    vector<CallbackType> callbacks;
};

But, to my surprise, the following “normal” version, which has only one “Argument Type”, does not compile:

template<typename RetType, typename ArgType>
class Event;

template<typename RetType, typename ArgType>
class Event<RetType(ArgType)> // <- error: wrong number of template arguments (1, should be 2)
{};

g ++ 4.6.1 gives an error, as in the comment.

Does anyone know why this is causing the error and how to make it work? Also, am I right in thinking that the above code is a form of "partial template specialization"?

+5
source share
3 answers
template<typename RetType, typename ArgType>
class Event;

It expects 2 template arguments, RetTypeand ArgType, you give only one RetType(ArgType).

template<typename RetType, typename... ArgType>
class Event;

Expects 1 or more template arguments, RetTypeand optional ArgTypes.

+4

std::function , :

template<class Signature>
class Event;

template<class R, class... Args>
class Event<R(Args...)>{
  // ...
};

, @ronag, (... ) . , , - , , std::/boost::function -like (int(int, double, char) - , , class Signature).

+5

, , , cpomiler , Event<templateArg1, templateArg2>, , .

On the specialization of the template: I do not agree, if I'm not mistaken what you are doing, you need to forward the class declaration Event, and then effectively declare it only two lines later.

+1
source

All Articles