Template specialization

I have an event system like this:
1
2
3
4
5
6
7
8
// Default template
template <class... P>
class Event{
public:
  void bind(std::function<void(P...)> callback);
  void trigger(P... args);
  void clear();
}

How can I specialize only needed members of the Event template (bind, trigger) for a type, without specializing everything?
Last edited on
Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

template < bool /*allow_specialisation */, typename... P > struct event_base // primary template
{
    void foo( P... ) const { std::cout << "event(primary)::foo()\n" ; }
    void bar( P... ) const { std::cout << "event(primary)::bar()\n" ; }
    void foobar( P... ) const { std::cout << "event(primary)::foobar()\n" ; }
};

template <> struct event_base< true, char, int > // explicit specialisation
                    :  private event_base< false, char, int > // base class is the primary template
{
    // specialised foo
    void foo( char, int ) const { std::cout << "event(explicit specialisation)::foo()\n" ; }

    using event_base<false,char,int>::bar ; // delegate to inherited primary bar
    using event_base<false,char,int>::foobar ; // delegate to inherited primary foobar
};

template < typename... P > using event = event_base<true,P...> ;

int main()
{
    event<char,int> eci ;
    eci.foo( 'a', 5 ) ; // specialised
    eci.bar( 'a', 5 ) ; // primary
    eci.foobar( 'a', 5 ) ; // primary
}

http://coliru.stacked-crooked.com/a/17386035e6882c8a
Topic archived. No new replies allowed.