5

没有什么比旧的好的 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);
}

这在 g++(4.9.2 和 5.1.0)中失败。但是,如果使用旧的返回类型:

template <class R> auto f(R (X::*)(int) const) {}
//                        ^
//                        old return type

有用。

在 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
4

1 回答 1

1

正如问题中所指出的,这是一个 gcc 错误,已在版本 6 中修复

gcc.gnu.org/bugzilla/show_bug.cgi?id=69139

于 2020-05-20T03:45:17.760 回答