OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Is this simple has-member SFINAE technique compliant?

  • Thread starter Thread starter willj
  • Start date Start date
W

willj

Guest
I'm using a simple (not particularly new) technique to check for the existence of a class member of a particular name and type. The following code compiles fine in Clang, but MSVC reports that the static_assert failed.

Code:
template<typename T, T m>
struct SfinaeNonType
{
    typedef void Type;
};

struct Member
{
};

template<typename C, typename U = void>
struct HasMember
{
    static const bool RESULT = false;
};

template<typename C>
struct HasMember<C, typename SfinaeNonType<Member C::*, &C::member>::Type>
{
    static const bool RESULT = true;
};

struct A
{
    Member member;
};

static_assert(HasMember<A>::RESULT, ""); // msvc rejects

Is there anything non-compliant about this code, or is this simply a bug in MSVC?

If it's a bug, is there a simple workaround?

Curiously, MSVC is happy with (otherwise identical code) checking for the existence of a member function rather than a data member:

Code:
template<typename T, T m>
struct SfinaeNonType
{
    typedef void Type;
};

struct Member
{
};

template<typename C, typename U = void>
struct HasMember
{
    static const bool RESULT = false;
};

template<typename C>
struct HasMember<C, typename SfinaeNonType<Member (C::*)(), &C::member>::Type>
{
    static const bool RESULT = true;
};

struct A
{
    Member member();
};

static_assert(HasMember<A>::RESULT, ""); // msvc accepts
Continue reading...
 

Latest posts

Top