我对“C++ 编程语言第 4 期,28.4.4”中的这段代码感到困惑
template<typename T>
struct get_f_result {
private:
template<typename X>
static auto check(X const& x) −> decltype(f(x)); // can call f(x)
static substitution_failure check(...); // cannot call f(x)
public:
using type = decltype(check(std::declval<T>()));
};
我特别困惑的部分是这里的这一行:
static substitution_failure check(...); // cannot call f(x)
但我记得 ...
不能接受非 pod 类型?那么这是如何工作的呢?
最佳答案
...
可以采用任何类型;可能不支持传递非 POD 类型,但在语法上是有效的。
在这种情况下,函数调用未被求值(因为它仅在未求值的上下文中使用,作为 decltype
的操作数),因此没有未定义的行为,只是编译时尝试将函数调用与合适的重载相匹配。
关于c++ - 对模板替换示例感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29913940/