Skip to content

Instantly share code, notes, and snippets.

@suxue
Created June 23, 2014 05:06
Show Gist options
  • Save suxue/ea88937628795bd6fa7d to your computer and use it in GitHub Desktop.
Save suxue/ea88937628795bd6fa7d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <type_traits>
#include <utility>
using namespace std;
void print(const string& str) {
cout << str << endl;
}
template<typename T>
struct printable {
template<typename C>
static char test(typename add_pointer<decltype(print(declval<C>()))>::type);
template<typename C>
static double test(...);
enum { value = sizeof(test<T>(nullptr)) == 1 };
};
template<typename T>
bool check_if_printable() {
return printable<T>::value;
}
int main(void)
{
cout << "string " << check_if_printable<string>() << endl;
cout << "const char* " << check_if_printable<const char*>() << endl;
cout << "char[2] " << check_if_printable<char[2]>() << endl;
cout << "int " << check_if_printable<int>() << endl;
cout << "double " << check_if_printable<double>() << endl;
cout << "const string& " << check_if_printable<const string&>() << endl;
cout << "string& " << check_if_printable<string&>() << endl;
cout << "string&& " << check_if_printable<string&&>() << endl;
cout << "const char*& " << check_if_printable<const char*&>() << endl;
cout << "char*& " << check_if_printable<char*&>() << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment