【发布时间】:2017-05-10 18:58:05
【问题描述】:
为什么
struct MyStruct {
auto foo () { return 1; }
auto bar () { return foo(); }
};
编译,但是当使用尾随返回类型时:
struct MyStruct {
auto foo () { return 1; }
auto bar () -> decltype(foo()) { return foo(); }
};
编译失败
错误:推导返回类型的函数'foo'在定义之前不能使用
在实现部分这是正确的行为吗?
【问题讨论】:
-
是的。成员函数体在逻辑上遵循类定义,即使是内联定义的。
-
好吧,如果你可以对
foo的返回类型使用简单的auto推导,为什么不将它也用于bar呢? -
我看不到
decltype的意义。我错过了什么吗?你在找auto& foo () { return i; } auto bar () ->decltype(auto) { return foo(); }吗?
标签: c++ c++14 language-lawyer return-type-deduction