Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workaround GCC 5.5 tuple initialisation bug

Tags:

c++

gcc

Consider this code:

  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}},
      };

In Clang, and GCC 6 or later it compiles fine. In GCC 5.5 it gives this error:

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

    };

    ^

Why is this and how can I work around it?

like image 977
Timmmm Avatar asked Sep 01 '26 03:09

Timmmm


1 Answers

One possible workaround is to call tuple constructor explicitly:

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)

like image 86
HolyBlackCat Avatar answered Sep 02 '26 19:09

HolyBlackCat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!