相关疑难解决方法(0)

使用私有成员变量返回类型推导

正如昨天的问答中所解释的那样,g ++ 4.8和Clang 3.3都正确地抱怨下面的代码,并且在这个范围内未声明"'b_'未声明"

#include <iostream>

class Test
{
public:
    Test(): b_(0) {}

    auto foo() const -> decltype(b_) // just leave out the -> decltype(b_) works with c++1y
    { 
        return b_;    
    }    
private:
    int b_;
};

int main() 
{
    Test t;
    std::cout << t.foo();
}
Run Code Online (Sandbox Code Playgroud)

将该private部分移动到类定义的顶部可消除错误并打印0.

我的问题是,这个错误在C++ 14中是否会随着返回类型推断而消失,这样我就可以省略decltypeprivate在类定义的末尾使用我的部分?

注意:它实际上是基于@JesseGood的答案.

c++ decltype c++11 return-type-deduction c++14

18
推荐指数
2
解决办法
835
查看次数

标签 统计

c++ ×1

c++11 ×1

c++14 ×1

decltype ×1

return-type-deduction ×1