c++ - decltype 作为类成员函数中的返回类型

标签 c++ c++11 decltype

我在下面的代码编译时出错。

struct B{
    double operator()(){
        return 1.0;
    }
};

struct A {
    auto func() -> decltype(b())
    {
        return b();
    }

    B b;
};

但是,如果我重新组织 A,它会编译。

gcc 4.8 表示 'b' 未在此范围内声明。

struct A {
    B b;
    auto func() -> decltype(b())
    {
        return b();
    }
};

那么,第一个有什么问题?

最佳答案

Is it valid?

您的最后一个示例格式正确,而第一个示例格式不正确(因此 GCC 是正确的)。

关于不合格名称查找的第 3.4.1/7 段规定:

A name used in the definition of a class X outside of a member function body, default argument, brace-or- equal-initializer of a non-static data member, or nested class definition shall be declared in one of the following ways:

before its use in class X or be a member of a base class of X (10.2), or

— [...]

以下是不适用于您的情况的其他条件。

关于c++ - decltype 作为类成员函数中的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16666304/

相关文章:

c++ - decltype(auto) 有哪些用途?

c++ - 从另一个模板类实例创建模板类实例时省略模板参数

c++ - 我的(基于指针的)合并排序有什么问题?

python - 在 C++ 中调用 Python

c++ - std::rethrow_exception 因嵌套线程而失败

c++ - 错误 : expected ')' before '&' token

c++ - 转换 yuv 4 :2:0 file to Rgb not getting expected output

c++ - 为什么这个公共(public)成员函数不能在类内声明的私有(private)结构成员上调用 decltype?

c++ - 在 C++ 中实现枚举类型

c++ - 在 C++ 中计算字符串中的字符出现次数