Skip to content

Instantly share code, notes, and snippets.

@makulik
Last active August 29, 2015 13:56
Show Gist options
  • Save makulik/8943582 to your computer and use it in GitHub Desktop.
Save makulik/8943582 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <typeinfo>
using namespace std;
template<typename FalseType, typename TrueType, bool condition>
struct ConditionalTypeSelector
{
typedef void ResultType;
};
template<typename FalseType, typename TrueType>
struct ConditionalTypeSelector<FalseType,TrueType,false>
{
typedef FalseType ResultType;
};
template<typename FalseType, typename TrueType>
struct ConditionalTypeSelector<FalseType,TrueType,true>
{
typedef TrueType ResultType;
};
struct A {
unsigned char member;
};
struct B {
int member;
};
struct C {
long long member;
};
int main() {
cout << typeid
( ConditionalTypeSelector
< A,B,(sizeof(A) > sizeof(B))
>::ResultType
).name() << endl;
cout << typeid
( ConditionalTypeSelector
< A,B,(sizeof(B) > sizeof(A))
>::ResultType
).name() << endl;
cout << typeid
( ConditionalTypeSelector
< A,C,(sizeof(A) > sizeof(C))
>::ResultType
).name() << endl;
cout << typeid
( ConditionalTypeSelector
< C,B,true>::ResultType
).name() << endl;
cout << typeid
( ConditionalTypeSelector
< C,A,false>::ResultType
).name() << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment