使用 std::make_tuple 时如何避免构造函数的未定义执行顺序

2022-01-19 00:00:00 tuples c++ c++11 variadic-templates

如果构造函数的执行顺序很重要,我该如何使用 std::make_tuple?

How can I use std::make_tuple if the execution order of the constructors is important?

比如我猜A类的构造函数和B类的构造函数的执行顺序是未定义的:

For example I guess the execution order of the constructor of class A and the constructor of class B is undefined for:

std::tuple<A, B> t(std::make_tuple(A(std::cin), B(std::cin)));

我在阅读了对该问题的评论后得出了这个结论

I came to that conclusion after reading a comment to the question

将 std::tuple 转换为模板参数包

这就是说这个

template<typename... args>
std::tuple<args...> parse(std::istream &stream) {
  return std::make_tuple(args(stream)...);
}

实现具有未定义的构造函数执行顺序.

implementation has an undefined execution order of the constructors.

更新,提供一些上下文:

为了给我想要做的事情提供更多背景知识,这里有一个草图:

To give some more background to what I am trying to do, here is a sketch:

我想在 stdin 中读取一些序列化对象nofollow noreferrer">CodeSynthesis XSD 二进制解析/序列化.以下是如何完成此类解析和序列化的示例:example/cxx/tree/binary/xdr/driver.cxx

I want to read in some serialized objects from stdin with the help of CodeSynthesis XSD binary parsing/serializing. Here is an example of how such parsing and serialization is done: example/cxx/tree/binary/xdr/driver.cxx

xml_schema::istream<XDR> ixdr (xdr); 
std::auto_ptr<catalog> copy (new catalog (ixdr));

我希望能够指定序列化对象具有的类的列表(例如,3 个序列化对象的目录、目录、someOtherSerializableClass)并将该信息存储为 typedef

I want to be able to specify a list of the classes that the serialized objects have (e.g. catalog, catalog, someOtherSerializableClass for 3 serialized objects) and store that information as a typedef

template <typename... Args>
struct variadic_typedef {};

typedef variadic_typedef<catalog, catalog, someOtherSerializableClass> myTypes;

如 是否可以在不展开模板参数包的情况下存储"它?

并找到一种在解析完成后获取 std::tuple 的方法.草图:

and find a way to get a std::tuple to work with after the parsing has finished. A sketch:

auto serializedObjects(binaryParse<myTypes>(std::cin));

serializedObjects 的类型在哪里

where serializedObjects would have the type

std::tuple<catalog, catalog, someOtherSerializableClass>

推荐答案

简单的解决方案是不首先使用std::make_tuple(...)而是直接构造一个 std::tuple<...>:成员的构造函数被调用的顺序是明确定义的:

The trivial solution is not to use std::make_tuple(...) in the first place but to construct a std::tuple<...> directly: The order in which constructors for the members are called is well defined:

template <typename>
std::istream& dummy(std::istream& in) {
    return in;
}
template <typename... T>
std::tuple<T...> parse(std::istream& in) {
    return std::tuple<T...>(dummy<T>(in)...);
}

函数模板dummy<T>()只是用来扩展一些东西.该顺序由 std::tuple 中元素的构造顺序强加:

The function template dummy<T>() is only used to have something to expand on. The order is imposed by construction order of the elements in the std::tuple<T...>:

template <typename... T>
    template <typename... U>
    std::tuple<T...>::tuple(U...&& arg)
        : members_(std::forward<U>(arg)...) { // NOTE: pseudo code - the real code is
    }                                        //       somewhat more complex

根据下面的讨论和 Xeo 的评论,似乎更好的选择是使用

Following the discussion below and Xeo's comment it seems that a better alternative is to use

template <typename... T>
std::tuple<T...> parse(std::istream& in) {
    return std::tuple<T...>{ T(in)... };
}

使用大括号初始化是有效的,因为大括号初始化列表中参数的求值顺序是它们出现的顺序.T{...} 的语义在 12.6.1 [class.explicit.init] 第 2 段中描述,说明它遵循列表初始化语义的规则(注意:这与std::initializer_list 仅适用于同质类型).排序约束在 8.5.4 [dcl.init.list] 第 4 段中.

The use of brace initialization works because the order of evaluation of the arguments in a brace initializer list is the order in which they appear. The semantics of T{...} are described in 12.6.1 [class.explicit.init] paragraph 2 stating that it follows the rules of list initialization semantics (note: this has nothing to do with std::initializer_list which only works with homogenous types). The ordering constraint is in 8.5.4 [dcl.init.list] paragraph 4.

相关文章