【问题标题】:C++11 std::tuple to std::array conversion causes variadic template crashC++11 std::tuple 到 std::array 的转换导致可变参数模板崩溃
【发布时间】:2012-10-18 22:16:28
【问题描述】:

以下函数 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;
}

【问题讨论】:

  • 看起来不错。我认为这是一个错误。如果是这样,你应该报告它。
  • 即使它无效,也不应该使编译器崩溃。 :) 为此提交一个错误。
  • @GManNickG:同意:现在正在处理。

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


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 2012-05-23
    • 1970-01-01
    • 2012-12-11
    • 2023-03-10
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多