SFINAE - 尝试确定模板类型是否具有带有'variable'返回类型的成员函数

时间:2012-04-26 19:57:35

标签: c++ templates visual-c++ c++11 sfinae

遇到SFINAE问题。我需要能够确定Type是否具有成员函数operator->无论其返回类型如何定义。示例如下。

测试中的这个类。它定义了operator->(),其返回类型为X *。因此我不知道“X”到处硬编码是什么。

template <class X>
class PointerX
{
    ...

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

此类尝试确定传入的T是否具有方法运算符 - &gt;定义;不管是什么操作员 - >返回类型是。

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);
};

除了operator-&gt;之外,这个类与上面完全相同。返回类型必须是'对象'。

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);
};

结果:

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的成员函数“Object operator-&gt;()const”。 所以它使用Test的通用版本Test(...)。

然而,HasOperatorMemberAccessorOBJECT能够找到PointX的“Object operator-&gt;()const”。 因此它使用Test专业版测试(SFINAE *)。

两者都应该能够找到“Object operator-&gt;()const”方法;因此两者都应该使用Test的专业版Test(SFINAE *);因此HasOperatorMemberAccessor&gt; :: value应为true。

HasOperatorMemberAccessor和HasOperatorMemberAccessorOBJECT之间的唯一区别是HasOperatorMemberAccessorOBJECT的类型名称R硬编码到对象,

所以问题是“decltype(GetReturnType(&amp; U :: operator-&gt;))”没有正确返回Object。我尝试了许多发现返回类型的不同许可。它们如下:

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

没有工作,为什么?我正在使用MSVC ++ 10.0。

1 个答案:

答案 0 :(得分:4)

您是否在询问如何实现这样的特性,或者为什么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"
    );
}

VC ++ 2010缺乏必要的C ++ 11工具(例如表达式SFINAE),以使其更加清洁。

相关问题