在对象初始化中是否允许指向 this 成员的指针?
Are pointer to a member of this allowed in object initialization?
来自,以下代码是否合法:
struct S
{
int a;
int* aptr;
};
S s = { 3, &s.a };
引自最新标准草案:
[basic.scope.pdecl]
The point of declaration for a name is immediately after its complete declarator ([dcl.decl]) and before its initializer (if any), except as noted below.
所以,是的。标识符 s
已经声明,因此可以在其初始化程序中使用。
请注意,s
的值在初始化之前可能无法使用。该值未在示例中使用,因此这不是问题。
I'd also be curious about whether analogous code is valid when the two members of S are in reversed order
成员的顺序无关紧要。
来自
struct S
{
int a;
int* aptr;
};
S s = { 3, &s.a };
引自最新标准草案:
[basic.scope.pdecl]
The point of declaration for a name is immediately after its complete declarator ([dcl.decl]) and before its initializer (if any), except as noted below.
所以,是的。标识符 s
已经声明,因此可以在其初始化程序中使用。
请注意,s
的值在初始化之前可能无法使用。该值未在示例中使用,因此这不是问题。
I'd also be curious about whether analogous code is valid when the two members of S are in reversed order
成员的顺序无关紧要。