Skip to content

Instantly share code, notes, and snippets.

@ddemidov
Forked from headmyshoulder/gist:3992253
Created November 1, 2012 07:05
Show Gist options
  • Save ddemidov/3992260 to your computer and use it in GitHub Desktop.
Save ddemidov/3992260 to your computer and use it in GitHub Desktop.
sfinae enabler
/*
* third.cpp
* Date: 2012-11-01
* Author: Karsten Ahnert (karsten.ahnert@gmx.de)
*/
#include <iostream>
#include <boost/utility/enable_if.hpp>
#define tab "\t"
using namespace std;
template <int N>
struct A { };
template <int N>
struct B { };
template< class T > struct is_a_or_b {};
template<int N> struct is_a_or_b< A<N> > { const static bool value = true; };
template<int N> struct is_a_or_b< B<N> > { const static bool value = true; };
template< class Class , class Enabler = void > struct dispatcher;
template< class T >
struct dispatcher< T , typename boost::enable_if_c< is_a_or_b< T >::value > ::type >
{
const static size_t value = 2;
};
template<int N>
struct dispatcher< A<N> >
{
const static size_t value = 1;
};
int main( int argc , char *argv[] )
{
cout << dispatcher< A<1> >::value << endl;
cout << dispatcher< B<1> >::value << endl;
return 0;
}
@ddemidov
Copy link
Author

ddemidov commented Nov 1, 2012

gistfile1.cpp:43:13: error: ambiguous partial specializations of 'dispatcher<A<1>, void>'
    cout << dispatcher< A<1> >::value << endl;
            ^
gistfile1.cpp:28:8: note: partial specialization matches [with T = A<1>]
struct dispatcher< T , typename boost::enable_if_c< is_a_or_b< T >::value > ::type >
       ^
gistfile1.cpp:34:8: note: partial specialization matches [with N = 1]
struct dispatcher< A<N> >
       ^
1 error generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment