当我尝试decltype()
在私有方法函数上使用时,我收到私有方法的错误error: 'm1' has not been declared in this scope
#include <stdint.h>
class C
{
public:
C()=default;
~C()=default;
auto masterMethod(int opMode) ->decltype(m1())
{
switch(opMode)
{
case 1:
return m1(); break;
case 2:
return m2(); break;
default:
return m1(); break;
}
}
private:
int m1() {return 1;}
int m2() {return 2;}
};
现在我的问题是,为什么编译器不在类的私有部分中查找,因为删除尾随返回类型或将私有部分放在顶部masterMethod
可以解决问题(decltype(auto)
[如果允许 C++14] 也可以正确的自动返回类型扣除)。
decltype(m1())
此外,删除whenm1()
并具有相同的返回类型是否是不好的行为m2()
,因为这也对我有用?