Convert std::tuple to std::array C++11

后端 未结 7 1062
情话喂你
情话喂你 2020-11-27 17:00

If I have std::tuple (where the type is homogeneous), is there a stock function or constructor to convert to std::array

7条回答
  •  感动是毒
    2020-11-27 17:40

    I would return the array instead of populating it by reference, so that auto can be used to make the callsite cleaner:

    template
    std::array
    fill_array_from_tuple(const std::tuple& t) {
      std::array arr;
      ArrayFiller::fill_array_from_tuple(t, arr);
      return arr;
    }
    
    // ...
    
    std::tuple tup(0.1, 0.2, 0.3);
    auto arr = fill_array_from_tuple(tup);
    

    Realistically, NRVO will eliminate most performance concerns.

提交回复
热议问题