C ++ std :: tuple销毁顺序 [英] C++ std::tuple order of destruction

查看:133
本文介绍了C ++ std :: tuple销毁顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一条规则规定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:

具有两个参数的元组的实例化类似于 具有相同两个参数的对的实例化.

An instantiation of tuple with two arguments is similar to an instantiation of pair with the same two arguments.

类似在这里不被解释为相同,因此,这并不意味着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销毁顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆