通过获取对子对象的引用来延长临时对象的寿命
Extending the life of a temporary object by getting a reference to a subobject
在 cppreference.com and by the C++11 Standard 中使用的关于何时延长临时对象的生命周期的语言之间存在细微差别(强调我的)。
Whenever a reference is bound to a temporary or to a base subobject of a temporary, the lifetime of the temporary is extended to match the lifetime of the reference,
The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except:
按照标准,a subject can be a member subject, a base class subject, or an array element.
如果我们严格按照以下示例代码中的标准措辞
struct Foo
{
Foo() : a(10), b(20) {}
~Foo() { std::cout << "In Foo::~Foo()\n"; }
int a;
int b;
};
Foo getFoo()
{
return Foo();
}
void testFoo1()
{
int const& r = getFoo().a;
std::cout << "In testFoo1()\n";
(void)r; // Shut up the compiler
}
getFoo()
返回的对象的生命周期应该延长引用的生命周期。但是,a simple test好像表示不是。
标准使用的措辞是否有缺陷?
编译器是否不符合规范?
cppreference.com 的措辞是否有缺陷?
更新
cppreference.com 中使用的语言已更新为(强调我的):
Whenever a reference is bound to a temporary or to a subobject thereof, the lifetime of the temporary is extended to match the lifetime of the reference, with the following exceptions:
Is the verbiage used by the standard a defect?
没有
Is the compiler non-conformant?
是的。正如评论中指出的那样,它已在较新版本的 g++ g++ 7 中得到修复。相关 link:http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54293.
Is the verbiage used by cppreference.com a defect?
是的。 the page at cppreference.com 的内容已更新。
在 cppreference.com and by the C++11 Standard 中使用的关于何时延长临时对象的生命周期的语言之间存在细微差别(强调我的)。
Whenever a reference is bound to a temporary or to a base subobject of a temporary, the lifetime of the temporary is extended to match the lifetime of the reference,
The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except:
按照标准,a subject can be a member subject, a base class subject, or an array element.
如果我们严格按照以下示例代码中的标准措辞
struct Foo
{
Foo() : a(10), b(20) {}
~Foo() { std::cout << "In Foo::~Foo()\n"; }
int a;
int b;
};
Foo getFoo()
{
return Foo();
}
void testFoo1()
{
int const& r = getFoo().a;
std::cout << "In testFoo1()\n";
(void)r; // Shut up the compiler
}
getFoo()
返回的对象的生命周期应该延长引用的生命周期。但是,a simple test好像表示不是。
标准使用的措辞是否有缺陷?
编译器是否不符合规范?
cppreference.com 的措辞是否有缺陷?
更新
cppreference.com 中使用的语言已更新为(强调我的):
Whenever a reference is bound to a temporary or to a subobject thereof, the lifetime of the temporary is extended to match the lifetime of the reference, with the following exceptions:
Is the verbiage used by the standard a defect?
没有
Is the compiler non-conformant?
是的。正如评论中指出的那样,它已在较新版本的 g++ g++ 7 中得到修复。相关 link:http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54293.
Is the verbiage used by cppreference.com a defect?
是的。 the page at cppreference.com 的内容已更新。