本地 class 规则是否与 c++14 return 类型推导一致?
Are local class rules aligned to c++14 return type deduction?
在阅读 C++14 的这一部分时 (a free draft N4141, closest to C++14):
9.8 Local class declarations [class.local]
[..]
The name of a local class is local to its enclosing scope. [..]
Declarations in a local class shall not odr-use (3.2) a variable with
automatic storage duration from an enclosing scope. [ Example:
//[..]
void f()
{
static int s ;
int x;
// [..]
struct local {
int g() { return x; } // error: odr-use of automatic variable x
int h() { return s; } // OK
// [..]
};
}
local* p = 0; // error: local not in scope
—end example ]
我注意到,首先 - 我可以用 return 值自动扣除来定义 p
:
auto f()
{
static int s;
int x;
struct local
{
int h() { return s; }
};
return local{};
}
decltype(f())* p = 0; // OK - ignored, that local is not in scope!
也许看起来没问题,为什么不通过从函数 return 值中推导它来使用本地类型,但是 - 似乎这样我可以在构造之前访问本地 s
变量:
struct TalkativeInt
{
TalkativeInt() : value()
{
std::cout << "TalkativeInt()\n";
}
TalkativeInt(int value) : value(value)
{
std::cout << "TalkativeInt(" << value << ")\n";
}
int value;
};
auto f()
{
static TalkativeInt s = 7;
int x;
struct local
{
auto h() { return s.value; }
};
return local{};
}
decltype(f())* p = 0;
int main() {
decltype(f()) l;
std::cout << l.h();
}
输出只是::
0
但人们可能会期望:
TalkativeInt(7)
7
clang 和 gcc 都不以任何方式抗议,参见 demo。
我想知道,也许这种情况应该在
中以某种方式提及
9.8 Local class declarations [class.local]
或在
7.1.6.4 auto specifier [dcl.spec.auto]
?
当然,我觉得在构造变量之前读取(和写入)变量是坏事,但我在标准中没有找到任何相关内容 - 可能,它在 C++14 之前是不可能的?或者有一些我刚刚忽略的基本规则?
静态局部变量的规则是plain and simple:
Dynamic initialization of a block-scope variable with static storage
duration (3.7.1) or thread storage duration (3.7.2) is performed the
first time control passes through its declaration; such a variable is
considered initialized upon the completion of its initialization.
并且 [basic.life]/(7.1) 禁止在构造对象之前访问对象的成员。
在阅读 C++14 的这一部分时 (a free draft N4141, closest to C++14):
9.8 Local class declarations [class.local]
[..] The name of a local class is local to its enclosing scope. [..]
Declarations in a local class shall not odr-use (3.2) a variable with automatic storage duration from an enclosing scope. [ Example:
//[..] void f() { static int s ; int x; // [..] struct local { int g() { return x; } // error: odr-use of automatic variable x int h() { return s; } // OK // [..] }; } local* p = 0; // error: local not in scope
—end example ]
我注意到,首先 - 我可以用 return 值自动扣除来定义 p
:
auto f()
{
static int s;
int x;
struct local
{
int h() { return s; }
};
return local{};
}
decltype(f())* p = 0; // OK - ignored, that local is not in scope!
也许看起来没问题,为什么不通过从函数 return 值中推导它来使用本地类型,但是 - 似乎这样我可以在构造之前访问本地 s
变量:
struct TalkativeInt
{
TalkativeInt() : value()
{
std::cout << "TalkativeInt()\n";
}
TalkativeInt(int value) : value(value)
{
std::cout << "TalkativeInt(" << value << ")\n";
}
int value;
};
auto f()
{
static TalkativeInt s = 7;
int x;
struct local
{
auto h() { return s.value; }
};
return local{};
}
decltype(f())* p = 0;
int main() {
decltype(f()) l;
std::cout << l.h();
}
输出只是::
0
但人们可能会期望:
TalkativeInt(7)
7
clang 和 gcc 都不以任何方式抗议,参见 demo。
我想知道,也许这种情况应该在
中以某种方式提及9.8 Local class declarations [class.local]
或在
7.1.6.4 auto specifier [dcl.spec.auto]
?
当然,我觉得在构造变量之前读取(和写入)变量是坏事,但我在标准中没有找到任何相关内容 - 可能,它在 C++14 之前是不可能的?或者有一些我刚刚忽略的基本规则?
静态局部变量的规则是plain and simple:
Dynamic initialization of a block-scope variable with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization.
并且 [basic.life]/(7.1) 禁止在构造对象之前访问对象的成员。