C ++ 11 std :: tuple到std :: array转换导致可变参数模板崩溃

时间:2012-10-18 22:16:29

标签: c++ templates c++11 tuples variadic-templates

以下函数toArray可能有一天会将C ++ 11 std::tuple转换为C ++ 11 std::array

#include <tuple>
#include <array>

template <typename T, typename ...U>
std::array<T,sizeof...(U)>
toArray(std::tuple<U...>) {
  return std::array<T,sizeof...(U)>();
}

如果我尝试使用以下代码调用toArray,在G ++ 4.8下我可以成功编译。然而,使用Clang ++ 3.2进行编译会导致Clang前端崩溃。我的代码是否有效C ++?

int main(int argc, char *argv[])
{
  auto tup = std::make_tuple(1,2,3,4,5,6,7,8);
  toArray<int>(tup);
  return 0;
}

1 个答案:

答案 0 :(得分:1)

它看起来对我有用,并且完成的版本适用于G ++:

#include <redi/index_tuple.h>
template <typename T, typename... U, unsigned... N>
  std::array<T, sizeof...(U)>
  toArray2(std::tuple<U...>& t, redi::index_tuple<N...>) {
    return std::array<T, sizeof...(U)>{{ std::get<N>(t)... }};
  }

template <typename T, typename ...U>
  std::array<T, sizeof...(U)>
  toArray(std::tuple<U...> t) {
    return toArray2<T>(t, redi::to_index_tuple<U...>{});
  }

int main()
{
  auto tup = std::make_tuple(1,2,3,4,5,6,7,8);
  return toArray<int>(tup)[3] - 4;
}