Has_f<> : checking if a type has a member with a specific name

Stroustrup describes a metaprogramming technique to check if a function f() takes an argument x of type X.
ie, it checks if the call f(x) is valid or not.

Using this technique, we can craft a type function "has_f" that performs this check - viz, if the call f(x) is valid or not.

Also, in order to use conventional function calling syntax, we can craft a type property predicate Has_f() that invokes the has_f type function:

1
2
3
4
5
   template<typename A>
   constexpr bool Has_f()
   {
      return has_f<A>::value;	/// bool
   }


Now, we can use the Has_f<A>() function to check if the program has a function f(A):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct A
{
};

void f(A)
{
   cout << "f(A)" << endl;
}


void test()
{
   if (Has_f<A>())
      cout << "The program has f(A)." << endl;
   else
      cout << "The program has NO f(A)." << endl;

   /// ...
}


This code works fine to check if the program has a function f(A) in the global namespace.

But, I would like to use this technique to determine if a type T has a function f(A):

1
2
3
4
class T
{
   void f(A);
}


How can the technique be adapted for checking if a type T has a function f?

Thanks.
Topic archived. No new replies allowed.