SFINAE β€” An attempt to determine if a template type has a member function with a variable return type

Having problems with SFINAE. I need to determine if a type has an operator-member-function defined independently of its return type. Example.

This class is in test. It defines an operator β†’ () with return type X *. Thus, I will not know that the "X" hardcodes for this.

template <class X>
class PointerX
{
    ...

    X* operator->() const;
    ...
}

This class is trying to determine if the method passed to T has operator-> define; regardless of type of operator-> return.

template<typename T>
struct HasOperatorMemberAccessor
{
    template <typename R, typename C> static R GetReturnType( R ( C::*)()const);

    template<typename U, typename R, R(U::*)()const> struct SFINAE{};
    template<typename U> static char Test(SFINAE<U,     decltype( GetReturnType(&U::operator->)),   &U::operator-> >*);
    template<typename U> static uint Test(...);
    static const bool value = sizeof(Test<T>(0)) == sizeof(char);
};

This class is the same as above, except that the type operator-> return must be "Object".

template<typename T>
struct HasOperatorMemberAccessorOBJECT
{
    template <typename R, typename C> static R GetReturnType( R ( C::*)()const);

    template<typename U, typename R, R(U::*)()const> struct SFINAE{};
    template<typename U> static char Test(SFINAE<U,     Object*,                &U::operator-> >*); // only change is we hardcoded Object as return type.
    template<typename U> static uint Test(...);
    static const bool value = sizeof(Test<T>(0)) == sizeof(char);
};

Results:

void main()
{
    HasOperatorMemberAccessor<PointerX<Object>>::Test<PointerX<Object>>(0);         // fails  ::value is false;  Test => Test(...)

    HasOperatorMemberAccessorOBJECT<PointerX<Object>>::Test<PointerX<Object>>(0);       // works! ::value is true;   Test => Test(SFINAE<>*)  
}

HasOperatorMemberAccessor PointX " β†’ () const". Test (...).

HasOperatorMemberAccessorOBJECT PointX " β†’ () const". , Test (SFINAE *).

"Object operator β†’ () const"; , , Test (SFINAE *); HasOperatorMemberAccessor > :: .

HasOperatorMemberAccessor HasOperatorMemberAccessorOBJECT , HasOperatorMemberAccessorOBJECT typename R, ,

, , "decltype (GetReturnType (& U:: operator- > ))" . . :

    decltype( GetReturnType(&U::operator->) )
    typename decltype( GetReturnType(&U::operator->))
    decltype( ((U*)nullptr)->operator->() )
    typename decltype( ((U*)nullptr)->operator->() )

, ? MSV++ 10.0.

+5
1

, , decltype , ? , :

#include <type_traits>

template<typename T, bool DisableB = std::is_fundamental<T>::value>
struct HasOperatorMemberAccessor
{ 
private:
    typedef char no;
    struct yes { no m[2]; };

    struct ambiguator { char* operator ->() { return nullptr; } };
    struct combined : T, ambiguator { };
    static combined* make();

    template<typename U, U> struct check_impl;
    template<typename U>
    static no check(
        U*,
        check_impl<char* (ambiguator::*)(), &U::operator ->>* = nullptr
    );
    static yes check(...);

public:
    static bool const value=std::is_same<decltype(check(make())), yes>::value;
};

// false for fundamental types, else the definition of combined will fail
template<typename T>
struct HasOperatorMemberAccessor<T, true> : std::false_type { };

// true for non-void pointers
template<typename T>
struct HasOperatorMemberAccessor<T*, false> :
    std::integral_constant<
        bool,
        !std::is_same<typename std::remove_cv<T>::type, void>::value
    >
{ };

template<typename X>
struct PointerX
{
    X* operator ->() const { return nullptr; }
};

struct X { };

int main()
{
    static_assert(
        HasOperatorMemberAccessor<PointerX<bool>>::value,
        "PointerX<> has operator->"
    );
    static_assert(
        !HasOperatorMemberAccessor<X>::value,
        "X has no operator->"
    );
    static_assert(
        HasOperatorMemberAccessor<int*>::value,
        "int* is dereferencable"
    );
    static_assert(
        !HasOperatorMemberAccessor<int>::value,
        "int is not dereferencable"
    );
    static_assert(
        !HasOperatorMemberAccessor<void*>::value,
        "void* is not dereferencable"
    );
}

V++ 2010 ++ 11 (, SFINAE), , .

+4

All Articles