SFINAE using scope resolution operator in decltype

Viewed 75

The question How does `void_t` work shows an example of SFINAE using void_t on a class data member T::member. However, it doesn't work if member is a function. Why is that the case ? It works if I change the code from decltype( T::member ) to decltype( T().member() ).

#include <iostream>

using namespace std;

template< class ... > using void_t = void;

template< class , class = void >
struct has_member : std::false_type
{ };

template< class T >
struct has_member< T , void_t< decltype( T::member ) > > : std::true_type
{ }; 

struct  A {
    void member();
};


int main()
{
    static_assert( has_member< A >::value , "A" ); // assertion fails

    return 0;
}
1 Answers

decltype( T::member ) doesn't work because T::member is not a valid expression expected by decltype when member refers to a non-static member function.

You can change it to decltype( &T::member ) instead. &T::member returns pointer to member, it works with both member functions and data members, static or non-static.

BTW: Why is “using namespace std;” considered bad practice?

Related