Skip to content

Instantly share code, notes, and snippets.

@kingsamchen
Last active December 16, 2015 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingsamchen/5432604 to your computer and use it in GitHub Desktop.
Save kingsamchen/5432604 to your computer and use it in GitHub Desktop.
demo for traits implementation
struct built_in_type_tag{};
struct integer_type_tag : public built_in_type_tag {};
struct user_defined_type_tag{};
template<typename TypeT>
struct type_traits
{
typedef typename TypeT::type_category type_category;
};
template<>
struct type_traits<int>
{
typedef integer_type_tag type_category;
};
class BLA
{
public:
class type_info
{
public:
typedef user_defined_type_tag type_category;
};
};
template<typename T>
string doTypeCheck(const T& obj, built_in_type_tag)
{
return "built-in type detected\n";
}
template<typename T>
string doTypeCheck(const T& obj, user_defined_type_tag)
{
return "user-defined type detected\n";
}
template<typename T>
string TypeCheck(const T& obj)
{
return doTypeCheck(obj, typename type_traits<T>::type_category());
}
int main()
{
int i;
BLA::type_info bla_into;
cout<<TypeCheck(i);
cout<<TypeCheck(bla_into);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment