Skip to content

Instantly share code, notes, and snippets.

Last active Feb 24, 2021
What would you like to do?
template <typename T>
class HasToString
typedef char YesType[1];
typedef char NoType[2];
template <typename C> static YesType& test( decltype(&C::ToString) ) ;
template <typename C> static NoType& test(...);
enum { value = sizeof(test<T>(0)) == sizeof(YesType) };
Copy link

fenbf commented Oct 10, 2019

Hi! we don't need bodies of the functions as it's used only at compile time. So we "Call" them without running the code... "0" converts to nullp pointer and that matches with the member function pointer &C::ToString. But if the ToString function is not available then we call default "NoType test(...) that matches all params.
You can also have a look at my updated post, with C++17 techniques:

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