c++ - 替换失败有时是一个错误

标签 c++ templates c++11 sfinae

似乎替换失败有时是一个错误。
有人能告诉我什么时候会出错,什么时候不会吗?
查看运行结果here
谢谢!

感谢您提供翔实的回答!
此代码无法使用 g++ 4.8 进行编译,但可以按预期使用 clang++ 3.2、g++ 4.7.3 和 intel 13.0.1。 所以现在我确定这是 g++ 4.8 的错误。我已将此报告给 gcc bugzila。

In substitution of ‘template<class C> static constexpr int has<T>::test(decltype (sizeof (C:: x))) [with C = C; T = foo] [with C = foo]’:
required from ‘const int has<foo>::value’
required from here
error: invalid use of non-static member function ‘std::string foo::x()’

代码

template <typename T>
struct has {
    template <typename>
    constexpr static int test(...) {
      return 0;
    }
    template <typename C>
    constexpr static int test(decltype(sizeof(C::x))) {  // Doesn't compile.
      return 1;   // Is a member variable.
    }
    template <typename C, int c =
        sizeof(decltype(((C*)nullptr)->x()))>
    constexpr static int test(int) {
      return 2;   // Is a member function.
    }
    static const int value = test<T>(0);
};

struct foo {
    string x();
};
struct bar {
    string x;
};
int main() {
    std::cout << has<int>::value << std::endl;
    std::cout << has<foo>::value << std::endl;
    std::cout << has<bar>::value << std::endl;
}

最佳答案

规则本质上是错误必须依赖于它直接所属的声明的模板参数。

这个例子看起来像一个编译器错误。它不能正确地排除特定语法缺陷。

如果你希望它在类有一个名为x的非静态成员函数时返回成功,你应该使用&C::x,因为你被允许采取指向成员函数的指针,但不只是将其命名为独立的子表达式。

我首选的方式是

template< typename, typename = void >
struct has_x
    : std::false_type {};

template< typename t >
struct has_x< t, typename std::enable_if< & t::x == & t::x >::type >
    : std::true_type {};

关于c++ - 替换失败有时是一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16016996/

相关文章:

c++ - 调整窗口大小会导致右边框附近出现污迹

visual-studio-2008 - 在VS2008中调试tt模板

c++ - 使用模板化声明时可能丢失基本对象的方法

c++ - 未使用的参数消除优化

c++ - 尝试将代码简化为移动语义的基于范围的 for 循环时,不匹配 ‘operator[]'

c++ - SDL 2 无法打开 Controller ,但可以识别操纵杆

c++ - 使用 __stdcall 和 Boost 1.47.0 编译错误 log4cxx

c++ - 未解析的外部符号 "std::atomic_fetch_add"

c++ - 为什么更喜欢基于模板的静态断言而不是基于 typedef 的静态断言?

c++ - "Default member initializer needed within definition of enclosing class outside of member functions"- 我的代码格式不正确吗?