evaluation order initialization array in c++

后端 未结 1 1377
青春惊慌失措
青春惊慌失措 2021-01-04 20:04

I like c++11 variadic templates, so I often write some little codes with it.

See this example:

#include 
#include 
#         


        
相关标签:
1条回答
  • 2021-01-04 20:25

    They are evaluated sequentially. C++11 § 8.5.4 [dcl.init.list] paragraph 4:

    Within the initializer-list of a braced-init-list, the initializer-clauses, including any that result from pack expansions (14.5.3), are evaluated in the order in which they appear.

    Given that vector has an initializer_list constructor, you could simplify your function to:

    template <typename ... T>
    auto make_vector(T ... t) ->
      std::vector< typename std::common_type<T...>::type >
    {
      return { static_cast<typename std::common_type<T...>::type>(t)... };
    }
    

    and not have to worry about arcane initialization semantics ;)

    0 讨论(0)
提交回复
热议问题