如何避免使用std :: make_tuple时构造函数的未定义执行顺序 [英] how to avoid undefined execution order for the constructors when using std::make_tuple

查看:214
本文介绍了如何避免使用std :: make_tuple时构造函数的未定义执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



例如,我猜测类A的构造函数的执行顺序, B类的构造函数未定义为:

  std :: tuple< A,B& T(std :: make_tuple(A(std :: cin),B(std :: cin))); 

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



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

 



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

实施有未定义的构造函数执行顺序。



更新,提供一些上下文:



为了给我一些更多的背景,这里是一个草图:



我想从 stdin 中读取一些序列化对象,借助 CodeSynthesis XSD 二进制解析/序列化。下面是一个如何解析和序列化的示例:example/cxx/tree/binary/xdr/driver.cxx

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

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

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

typedef variadic_typedef< catalog,catalog,someOtherSerializableClass> myTypes;

>是否可以存储模板参数包而不进行扩展?



并找到一种方法来获取std :: tuple解析完成后。草图:

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

其中serializedObjects的类型为

  std :: tuple< catalog,catalog,someOtherSerializableClass> 


解决方案

>首先使用 std :: make_tuple(...),但构造一个 std :: tuple< ...> / code>直接:成员的构造函数的调用顺序是明确定义的:

  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& / code>只用于有东西扩展。该顺序由 std :: tuple< T ...> 中的元素的构造顺序强制:

  template< typename ... T> 
template< typename ... U>
std :: tuple< T ...> :: tuple(U ...&& arg)
:members_(std :: forward< U> {//注意:伪代码 - 真正的代码是
} //比较复杂

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

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

使用括号初始化的原理是因为括号初始化列表是它们出现的顺序。第12.6.1节[class.explicit.init]中描述了 T {...} 的语义,声明它遵循列表初始化语义的规则(注意:这与std :: initializer_list无关,它只适用于同构类型)。顺序约束在8.5.4 [dcl.init.list]段落4中。


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

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

Translating a std::tuple into a template parameter pack

that says that this

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.

Update, providing some context:

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

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));

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;

as suggested in Is it possible to "store" a template parameter pack without expanding it?

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));

where serializedObjects would have the type

std::tuple<catalog, catalog, someOtherSerializableClass>

解决方案

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)...);
}

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

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)... };
}

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.

这篇关于如何避免使用std :: make_tuple时构造函数的未定义执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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