Skip to content

Instantly share code, notes, and snippets.

@headmyshoulder
Created November 1, 2012 07:03
Show Gist options
  • Save headmyshoulder/3992253 to your computer and use it in GitHub Desktop.
Save headmyshoulder/3992253 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;
struct A { };
struct B { };
template< class T > struct is_a_or_b {};
template<> struct is_a_or_b< A > { const static bool value = true; };
template<> struct is_a_or_b< B > { 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<>
struct dispatcher< A >
{
const static size_t value = 1;
};
int main( int argc , char *argv[] )
{
cout << dispatcher< A >::value << endl;
cout << dispatcher< B >::value << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment