解决方法GCC 5.5元组初始化错误

时间:2018-05-18 13:53:37

标签: c++ gcc

考虑以下代码:

  std::vector<
          std::tuple<std::vector<std::size_t>,
                     std::vector<std::size_t>,
                     std::vector<std::size_t>>
        > foo = {
        {{2, 1, 2, 3},   {1, 2},  {2, 3}},
        {{2, 3, 4, 0},   {3},     {2, 3, 4}},
        {{2, 3, 4, 0},   {0},     {3, 4, 0}},
      };

在Clang和GCC 6或更高版本中,它编译得很好。在GCC 5.5中,它给出了这个错误:

 In function 'int main()':

:16:4: error: converting to
          'std::tuple<std::vector<long unsigned int, std::allocator<long unsigned int> >,
                      std::vector<long unsigned int, std::allocator<long unsigned int> >,
                      std::vector<long unsigned int, std::allocator<long unsigned int> > >'
      from initializer list would use explicit constructor
          'constexpr std::tuple< <template-parameter-1-1> >::tuple(const _Elements& ...)
     [with _Elements = {
           std::vector<long unsigned int, std::allocator<long unsigned int> >, std::vector<long unsigned int, std::allocator<long unsigned int> >, std::vector<long unsigned int, std::allocator<long unsigned int> >}]'

    };

    ^

为什么会这样,我该如何解决?

1 个答案:

答案 0 :(得分:3)

一种可能的解决方法是明确调用tuple构造函数:

using bar = std::tuple<std::vector<std::size_t>,
                       std::vector<std::size_t>,
                       std::vector<std::size_t>>;
std::vector<bar> foo = {
    bar{{2, 1, 2, 3},   {1, 2},  {2, 3}},
    bar{{2, 3, 4, 0},   {3},     {2, 3, 4}},
    bar{{2, 3, 4, 0},   {0},     {3, 4, 0}},
};

(Live demo)

相关问题