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;
}
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.