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

前端 未结 5 1500
攒了一身酷
攒了一身酷 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:06

    I believe the only way to manually unroll the definition. Something like the following might work. I welcome attempts to make it nicer though.

    #include 
    #include 
    
    struct A { A(std::istream& is) {}};
    struct B { B(std::istream& is) {}};
    
    template  
    class Parser
    { };
    
    template 
    class Parser
    {
    public:
       static std::tuple parse(std::istream& is) {return std::make_tuple(T(is)); }
    };
    
    template 
    class Parser
    {
    public:
       static std::tuple parse(std::istream& is) 
       {
          A t(is);
          return std::tuple_cat(std::tuple(std::move(t)),
             Parser::parse(is));
       }
    };
    
    
    int main()
    {
       Parser::parse(std::cin);
       return 1;
    }
    

提交回复
热议问题