小编Mic*_*l G的帖子

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

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

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

template <class X>
class PointerX
{
    ...

    X* operator->() const;
    ...
}
Run Code Online (Sandbox Code Playgroud)

此类尝试确定传入的T是否具有方法operator-> defined; 无论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);
};
Run Code Online (Sandbox Code Playgroud)

除了operator-> return类型必须是'Object'之外,这个类与上面完全相同.

template<typename T>
struct HasOperatorMemberAccessorOBJECT
{
    template <typename R, typename C> static R …
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae visual-c++ c++11

5
推荐指数
1
解决办法
1944
查看次数

标签 统计

c++ ×1

c++11 ×1

sfinae ×1

templates ×1

visual-c++ ×1