问题描述
是否有一条规则规定std :: tuple的成员被销毁的顺序?
Is there a rule which states in which order the members of an std::tuple are destroyed?
例如,如果Function1
返回std::tuple<std::unique_ptr<ClassA>, std::unique_ptr<ClassB>>
到Function2
,那么我可以确定(当Function2
的范围保留时)第二个成员引用的ClassB
实例被销毁在第一个成员引用的ClassA
实例之前?
For example if Function1
returns an std::tuple<std::unique_ptr<ClassA>, std::unique_ptr<ClassB>>
to Function2
, then can I be sure that (when the scope of Function2
is left) the instance of ClassB
referred to by the second member is destroyed before the instance of ClassA
referred to by the first member?
std::tuple< std::unique_ptr< ClassA >, std::unique_ptr< ClassB > > Function1()
{
std::tuple< std::unique_ptr< ClassA >, std::unique_ptr< ClassB > > garbage;
get<0>(garbage).reset( /* ... */ );
get<1>(garbage).reset( /* ... */ );
return garbage;
}
void Function2()
{
auto to_be_destroyed = Function1();
// ... do something else
// to_be_destroyed leaves scope
// Is the instance of ClassB destroyed before the instance of ClassA?
}
推荐答案
标准未指定std::tuple
的销毁顺序. §20.4.1/p1指出以下事实:
The standard doesn't specify the order of destruction for std::tuple
. The fact that §20.4.1/p1 specifies that:
类似在这里不被解释为相同,因此,这并不意味着std::tuple
的参数应具有相反的销毁顺序.
Similar here is not interpreted as identical and consequently it's not implied that std::tuple
should have a reverse destruction order of its arguments.
鉴于std::tuple
的递归性质,最有可能的是破坏的顺序与其参数的顺序是一致的.
Given the recursive nature of std::tuple
most probable is that the order of destruction is in order with the order of its arguments.
我的假设也基于 GCC错误66699 的错误报告.我上面的假设是合理的.
I also base my assumptions on a bug report for GCC BUG 66699 where in the discussion my assumptions above are justified.
也就是说,std::tuple
的销毁顺序未指定.
That said, the order of destruction for std::tuple
is unspecified.
这篇关于C ++ std :: tuple销毁顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!