N
Noah Roberts
Trying to come up with a method to find out if a class contains a name
within it that is callable with certain variables, whether it's a
templated function or not.
Here's my attempt:
template < typename T > T&& declval(); // MSVC doesn't have.
template < typename T, typename Enable = void >
struct has_callable_f
: boost::mpl::false_
{};
template < typename T >
struct has_callable_f<T, decltype(declval<T>().f(declval<int>()))>
: boost::mpl::true_
{};
struct test
{
template < typename T >
void f() {}
};
int main()
{
static_assert(has_callable_f<test>::value, "blah");
}
I get an error indicating the decltype expression is invalid:
1>d:\dev_workspace\experimental\2010scratch\2010scratch\scratch.cpp(20):
error C2228: left of '.f' must have class/struct/union
1> type is 'T'
1>
If I put 'test' instead of 'T' (moving the declaration of course) then
it works.
Is what I'm trying to do legitimate?
within it that is callable with certain variables, whether it's a
templated function or not.
Here's my attempt:
template < typename T > T&& declval(); // MSVC doesn't have.
template < typename T, typename Enable = void >
struct has_callable_f
: boost::mpl::false_
{};
template < typename T >
struct has_callable_f<T, decltype(declval<T>().f(declval<int>()))>
: boost::mpl::true_
{};
struct test
{
template < typename T >
void f() {}
};
int main()
{
static_assert(has_callable_f<test>::value, "blah");
}
I get an error indicating the decltype expression is invalid:
1>d:\dev_workspace\experimental\2010scratch\2010scratch\scratch.cpp(20):
error C2228: left of '.f' must have class/struct/union
1> type is 'T'
1>
If I put 'test' instead of 'T' (moving the declaration of course) then
it works.
Is what I'm trying to do legitimate?