c++boostboost-signals

boost::signal slot_type with a template


I'm getting a build error when building for the following function:

template <class T> class Event {
public:
    typedef boost::signal<void (void*, const T)>    signal_t;
    typedef boost::signals::connection              connection_t;   

public:
    Event() { }

    connection_t connect(signal_t::slot_type subscriber){
        return signal.connect(subscriber);
    }
}

error:

error: 'class boost::signal<void ()(void*, T), boost::last_value<typename boost::function_traits<void ()(void*, T)>::result_type>, int, std::less<int>, boost::function<void ()(void*, T)> >::slot_type' is not a type

I believe it has something to do with the fact that it's templated, but I'm not sure how to fix it. I've tried adding , such that

connection_t connect(signal_t::slot_type<T> subscriber)

But this just produceds another error.


Solution

  • Tell the compiler that it's a type with typename:

    connection_t connect(typename signal_t::slot_type subscriber){
        return signal.connect(subscriber);
    }
    

    The problem is that signal_t depends on the template parameter T, and so you need to explicitly tell the compiler that signal_t::slot_type is going to be a type.