how to avoid undefined execution order for the constructors when using std::make_tuple

前端 未结 5 1486
攒了一身酷
攒了一身酷 2020-12-01 12:33

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

5条回答
  •  攒了一身酷
    2020-12-01 13:08

    There's nothing special about make_tuple here. Any function call in C++ allows its arguments to be called in an unspecified order (allowing the compiler freedom to optimize).

    I really don't suggest having constructors that have side-effects such that the order is important (this will be a maintenance nightmare), but if you absolutely need this, you can always construct the objects explicitly to set the order you want:

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

提交回复
热议问题