Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SFINAE: detect if class has free function

Is there a way, using SFINAE, to detect whether a free function is overloaded for a given class?

Basically, I’ve got the following solution:

struct has_no_f { };

struct has_f { };

void f(has_f const& x) { }

template <typename T>
enable_if<has_function<T, f>::value, int>::type call(T const&) {
    std::cout << "has f" << std::endl;
}

template <typename T>
disable_if<has_function<T, f>::value, int>::type call(T const&) {
    std::cout << "has no f" << std::endl;
}

int main() {
    call(has_no_f()); // "has no f"
    call(has_f()); // "has f"
}

Simply overloading call doesn’t work since there are actually a lot of foo and bar types and the call function has no knowledge of them (basically call is inside a and the users supply their own types).

I cannot use C++0x, and I need a working solution for all modern compilers.

Note: the solution to a similar question unfortunately doesn’t work here.

like image 897
Konrad Rudolph Avatar asked Jan 26 '11 14:01

Konrad Rudolph


2 Answers

#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>
#include <type_traits>

struct X {};
struct Y {};

__int8 f(X x) { return 0; }
__int16 f(...) { return 0; }

template <typename T> typename std::enable_if<sizeof(f(T())) == sizeof(__int8), int>::type call(T const& t) {
    std::cout << "In call with f available";
    f(t);
    return 0;
}

template <typename T> typename std::enable_if<sizeof(f(T())) == sizeof(__int16), int>::type call(T const& t) {
    std::cout << "In call without f available";
    return 0;
}

int main() {
    Y y; X x;
    call(y);
    call(x);
}

A quick modification of the return types of f() yields the traditional SFINAE solution.

like image 126
Puppy Avatar answered Sep 17 '22 20:09

Puppy


If boost is allowed, the following code might meet your purpose:

#include <boost/type_traits.hpp>
#include <boost/utility/enable_if.hpp>
using namespace boost;

// user code
struct A {};
static void f( A const& ) {}
struct B {};


// code for has_f
static void f(...); // this function has to be a free standing one

template< class T >
struct has_f {
  template< class U >
  static char deduce( U(&)( T const& ) );

  template< class U, class V >
  static typename disable_if_c< is_same< V, T >::value, char(&)[2] >::type
  deduce( U(&)( V const& ) );

  static char (&deduce( ... ))[2];

  static bool const value = (1 == sizeof deduce( f ));
};

int main()
{
  cout<< has_f<A>::value <<endl;
  cout<< has_f<B>::value <<endl;
}

However, there are severe restrictions.
The code assumes that all the user functions have the signature ( T const& ), so ( T ) isn't allowed.
The function void f(...) in the above seems to need to be a free standing function.
If the compiler enforces two phase look-up as expected normally, probably all the user functions have to appear before the definition of has_f class template.
Honestly, I'm not confident of the usefulness of the code, but anyway I hope this helps.

like image 40
Ise Wisteria Avatar answered Sep 21 '22 20:09

Ise Wisteria