C++11 では、この回答をstatic_assertと組み合わせて、より有用なコンパイラ エラー メッセージを提供できます。例えば:
#define HAS_MEM_FUNC(func, name) \
template <typename T> \
class name { \
typedef char one; \
typedef long two; \
template <typename C> static one test( decltype(&C::func)); \
template <typename C> static two test(...); \
public: \
enum { value = sizeof(test<T>(0)) == sizeof(char) }; \
}
HAS_MEM_FUNC(A, has_A);
HAS_MEM_FUNC(B, has_B);
template<class T>
class Foo
{
public:
void Fun()
{
static_assert(has_A<T>::value,
"Template parameter does not contain member function `A`.");
static_assert(has_B<T>::value,
"Template parameter does not contain member function `B`.");
mT.A();
mT.B();
}
T mT;
};
今コード
Foo<int> blah;
blah.Fun();
エラーメッセージが表示されます:
test.cpp:21:9: error: static assertion failed: Template parameter does not contain member function A.
test.cpp:23:9: error: static assertion failed: Template parameter does not contain member function B.
test.cpp:26:9: error: request for member ‘A’ in ‘((Foo<int>*)this)->Foo<int>::mT’, which is of non-class type ‘int’
test.cpp:27:9: error: request for member ‘B’ in ‘((Foo<int>*)this)->Foo<int>::mT’, which is of non-class type ‘int’