gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:13:11 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com