【问题标题】:Workaround GCC 5.5 tuple initialisation bug解决方法 GCC 5.5 元组初始化错误
【发布时间】:2018-10-28 23:42:32
【问题描述】:

考虑这段代码:

  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> >}]'

    };

    ^

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

【问题讨论】:

标签: c++ gcc


【解决方案1】:

一种可能的解决方法是显式调用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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 2020-12-09
    • 2012-06-04
    • 2015-01-10
    相关资源
    最近更新 更多