Ошибка компиляции gcc "привязка const s" к ссылке на тип & s "отбрасывает квалификаторы" при использовании boost :: container :: static_vector в другом контейнере - PullRequest
0 голосов
/ 14 сентября 2018

При использовании boost :: container :: static_vector в другом контейнере, таком как std :: vector, gcc возвращает ошибку компиляции.Следующий тестовый пример воспроизводит ошибку:

#include <boost/container/static_vector.hpp>
#include <vector>

struct s
{
    boost::container::static_vector<int,6> a;
};

int main()
{
    std::vector<s> b;
    b.resize(6);
}

Выполнено с g++ template_test2.cpp в Ubuntu Xenial приводит к следующей ошибке:

In file included from /usr/include/c++/5/bits/char_traits.h:39:0,
                 from /usr/include/c++/5/string:40,
                 from /usr/include/c++/5/stdexcept:39,
                 from /usr/include/boost/container/throw_exception.hpp:26,
                 from /usr/include/boost/container/new_allocator.hpp:24,
                 from /usr/include/boost/container/vector.hpp:28,
                 from /usr/include/boost/container/static_vector.hpp:25,
                 from template_test2.cpp:1:
/usr/include/c++/5/bits/stl_algobase.h: In instantiation of ‘typename __gnu_cxx::__enable_if<(! std::__is_scalar<_Tp>::__value), void>::__type std::__fill_a(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = s*; _Tp = s; typename __gnu_cxx::__enable_if<(! std::__is_scalar<_Tp>::__value), void>::__type = void]’:
/usr/include/c++/5/bits/stl_algobase.h:747:20:   required from ‘void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = s*; _Tp = s]’
/usr/include/c++/5/bits/vector.tcc:469:14:   required from ‘void std::vector<_Tp, _Alloc>::_M_fill_insert(std::vector<_Tp, _Alloc>::iterator, std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = s; _Alloc = std::allocator<s>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<s*, std::vector<s> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = s*; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = s]’
/usr/include/c++/5/bits/stl_vector.h:1073:23:   required from ‘void std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::iterator, std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = s; _Alloc = std::allocator<s>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<s*, std::vector<s> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = s*; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = s]’
/usr/include/c++/5/bits/stl_vector.h:716:10:   required from ‘void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type, std::vector<_Tp, _Alloc>::value_type) [with _Tp = s; _Alloc = std::allocator<s>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = s]’
template_test2.cpp:12:11:   required from here
/usr/include/c++/5/bits/stl_algobase.h:701:11: error: binding ‘const s’ to reference of type ‘s&’ discards qualifiers
  *__first = __value;

Эта ошибка возникает, только если struct s содержитстатический векторВ чем причина этой ошибки и есть ли обходной путь?

1 Ответ

0 голосов
/ 14 сентября 2018

Похоже, что оператор копирования по умолчанию, сгенерированный компилятором, имеет подпись

s& operator= (s&);

и ваш код завершается ошибкой в ​​этой строке алгоритма fill (он вызывается как-то в resize методе):

  for (; __first != __last; ++__first)
      *__first = __value;  // <----- call operator=(s&) passing const s& object

, поскольку __value является константной ссылкой на объект s и не может быть передан в качестве аргумента operator=(s&) (объект const нельзя изменить).

О генерации оператора копирования по умолчанию для классов вы можете прочитать здесь . В реализации static_vector есть что-то странное: оператор присваивания по умолчанию принимает аргумент s& вместо const s&.

Чтобы решить эту проблему, вы можете реализовать собственную версию этого оператора:

 s& operator = (const s& other) {
     a = other.a;
     return *this;
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...