bol*_*lov 5 c++ templates language-lawyer trailing-return-type c++14
没有什么比旧的MCVE更清楚了:
struct X {
auto get(int) const -> int { return {}; }
auto get(int) -> int { return {}; }
};
template <class R> auto f(auto (X::*)(int) const -> R) {}
// ^~~~ ~~~~
// trailing return type
int main() {
f(&X::get);
}
Run Code Online (Sandbox Code Playgroud)
这在g ++(4.9.2和5.1.0)中失败了.但是,如果使用旧的返回类型:
template <class R> auto f(R (X::*)(int) const) {}
// ^
// old return type
Run Code Online (Sandbox Code Playgroud)
有用.
在clang(3.5.0)上,两种变体都有效.
我知道尾部返回类型在推断返回类型时会发生变化及其范围,因此我不会很快将其作为gcc错误进行转换.那标准说的是什么?哪个编译器是对的?
我认为错误中最重要的信息是
无法推导出模板参数'R'
g ++完整消息:
main2.cpp: In function ‘int main()’:
main2.cpp:21:12: error: no matching function for call to ‘f(<unresolved overloaded function type>)’
f(&X::get);
^
main2.cpp:18:25: note: candidate: template<class R, class auto:1> auto f(auto:1 (X::*)(int) const)
template <class R> auto f(auto (X::*)(int) const -> R) {}
^
main2.cpp:18:25: note: template argument deduction/substitution failed:
main2.cpp:21:12: note: types ‘auto:1 (X::)(int) const’ and ‘int (X::)(int)’ have incompatible cv-qualifiers
f(&X::get);
^
main2.cpp:21:12: note: couldn't deduce template parameter ‘R’
<builtin>: recipe for target 'main2' failed
make: *** [main2] Error 1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
186 次 |
| 最近记录: |