The problem of the existence of a member function of SFINAE

I have this member function test:

template <typename T> 
struct has_member {
    template <typename U>  static true_type  f(decltype(declval<U>().member()) *);
    template <typename>    static false_type f(...);
    static const bool value =  decltype(f<T>(0))::value;
};

It evaluates to true when there is a member function with the given name, in case the function has an overload that takes no arguments. For such functions, and in the case of STL containers, it works correctly, except for the functions of access to elements (front, back, etc.), where it invariably evaluates to false.

Why? I have mingw g ++ 4.7.

+5
source share
1 answer

This is because these functions return links, and you declare a pointer to the return value, that is, a pointer to a link, and this is not possible.

Quick fix:

template <typename U>  static true_type  
        f(typename remove_reference< decltype(declval<U>().member()) >::type *);

PS. () , , SFINAE , , .

, false_type , true_type . :

test.cpp:9:50: error: forming pointer to reference type__gnu_cxx::__alloc_traits<std::allocator<int> >::value_type& {aka int&}’
+6

All Articles