Isn't reporting this as a useable boolean value and not a compile-time error the point of this trait structure?
Yes, but in order to test some member of a class, that member needs to exist.
You get the error because B
doesn't have boo
and instantiating alg
results in decltype(&B::boo)
. You wouldn't expect decltype(&int::foo)
to compile, would you?
What you need is a trait that check for existense of a member. See
- Is it possible to write a C++ template to check for a function's existence?
- Check if a class has a member function of a given signature
for examples.
You can use the result of the check to specialize the template that does further test.
EDIT:
Here's a simple way to do check for boo
using expression SFINAE:
template<typename T>
constexpr auto is_booable(int) -> decltype(std::declval<T>().boo(), bool())
{
return true;
}
template<typename T>
constexpr bool is_booable(...)
{
return false;
}
Usage:
booable< is_booable<Type>(0) >::booIt(t);
And Live example.