Std::span вектора не компилируетсяC++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Std::span вектора не компилируется

Сообщение Anonymous »

Давайте начнем с того, что еще раз заявим, что std::vector был ошибкой. Но теперь оно наконец укусило меня. Это не компилируется:
LiveDemo

Код: Выделить всё

#include 
#include 
#include 
#include 

int main()
{
std::vector intvec = { 1, 2, 3, 4 };
auto vec_int_view = std::span(intvec);

// This will not compile:
//    std::vector boolvec = { true, false, true, true };
//    auto vec_bool_view = std::span(boolvec);
}
Ошибки:

Код: Выделить всё

: In function 'int main()':
:13:43: error: class template argument deduction failed:
13 |     auto vec_bool_view = std::span(boolvec);
|                                           ^
:13:43: error: no matching function for call to 'span(std::vector&)'
In file included from :2:
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:233:9: note: candidate: 'template  requires (_Extent == std::dynamic_extent || _OExtent == std::dynamic_extent || _Extent == _OExtent) && (std::__is_array_convertible::value) span(const std::span&)-> std::span'
233 |         span(const span& __s) noexcept
|         ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:233:9: note:   template argument deduction/substitution failed:
:13:43: note:   'std::vector' is not derived from 'const std::span'
13 |     auto vec_bool_view = std::span(boolvec);
|                                           ^
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:225:7: note: candidate: 'template span(const std::span&)-> std::span'
225 |       span(const span&) noexcept = default;
|       ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:225:7: note:   template argument deduction/substitution failed:
:13:43: note:   'std::vector' is not derived from 'const std::span'
13 |     auto vec_bool_view = std::span(boolvec);
|                                           ^
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:213:9: note: candidate: 'template  requires !(__is_span) && !(__is_std_array) && !(is_array_v) && (contiguous_range) && (sized_range) && ((borrowed_range) || (is_const_v)) && (std::__is_compatible_ref::value) span(_Range&&)-> std::span'
213 |         span(_Range&& __range)
|         ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:213:9: note:   template argument deduction/substitution failed:
:13:43: note:   couldn't deduce template parameter '_Type'
13 |     auto vec_bool_view = std::span(boolvec);
|                                           ^
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:201:9: note: candidate: 'template  requires  std::span::__is_compatible_array::value span(const std::array&)-> std::span'
201 |         span(const array& __arr) noexcept
|         ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:201:9: note:   template argument deduction/substitution failed:
:13:43: note:   'std::vector' is not derived from 'const std::array'
13 |     auto vec_bool_view = std::span(boolvec);
|                                           ^
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:194:9: note: candidate: 'template  requires  std::span::__is_compatible_array::value span(std::array&)-> std::span'
194 |         span(array&  __arr) noexcept
|         ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:194:9: note:   template argument deduction/substitution failed:
:13:43: note:   'std::vector' is not derived from 'std::array'
13 |     auto vec_bool_view = std::span(boolvec);
|                                           ^
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:187:9: note: candidate: 'template  requires  _Extent == std::dynamic_extent || _ArrayExtent == _Extent span(std::type_identity_t (&)[_ArrayExtent])-> std::span'
187 |         span(type_identity_t (&__arr)[_ArrayExtent]) noexcept
|         ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:187:9: note:   template argument deduction/substitution failed:
:13:43: note:   mismatched types 'std::type_identity_t [_ArrayExtent]' and 'std::vector'
13 |     auto vec_bool_view = std::span(boolvec);
|                                           ^
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:172:9: note: candidate: 'template  requires (contiguous_iterator) && (sized_sentinel_for) && ((std::__is_compatible_ref::value) && !(is_convertible_v)) span(_It, _End)-> std::span'
172 |         span(_It __first, _End __last)
|         ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:172:9: note:   candidate expects 2 arguments, 1 provided
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:157:9: note: candidate: 'template  requires (contiguous_iterator) && (std::__is_compatible_ref::value) span(_It, std::size_t)-> std::span'
157 |         span(_It __first, size_type __count)
|         ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:157:9: note:   candidate expects 2 arguments, 1 provided
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:149:7: note: candidate: 'template span()-> std::span requires  _Extent == std::dynamic_extent || _Extent == 0'
149 |       span() noexcept
|       ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:149:7: note:   candidate expects 0 arguments, 1 provided
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:54:11: note: candidate: 'template span(std::span)-> std::span'
54 |     class span;
|           ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:54:11: note:   template argument deduction/substitution failed:
:13:43: note:   'std::vector' is not derived from 'std::span'
13 |     auto vec_bool_view = std::span(boolvec);
|                                           ^
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:466:5: note: candidate: 'template  requires  contiguous_range std::span(_Range&&)-> span'
466 |     span(_Range &&)
|     ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:466:5: note:   template argument deduction/substitution failed:
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:466:5: note: constraints not satisfied
In file included from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/compare:40,
from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/char_traits.h:56,
from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/ios:42,
from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/ostream:40,
from /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/iostream:41,
from :1:
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/concepts: In substitution of 'template  requires  contiguous_range std::span(_Range&&)-> span  [with _Range = std::vector&]':
:13:43:   required from here
13 |     auto vec_bool_view = std::span(boolvec);
|                                           ^
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/concepts:74:13:   required for the satisfaction of 'derived_from' [with _Iter = std::_Bit_iterator]
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/iterator_concepts.h:697:13:   required for the satisfaction of 'contiguous_iterator' [with _Tp = std::vector&]
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/ranges_base.h:607:13:   required for the satisfaction of 'contiguous_range' [with _Range = std::vector&]
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/concepts:74:28: note:   'std::contiguous_iterator_tag' is not a base of 'std::random_access_iterator_tag'
74 |     concept derived_from = __is_base_of(_Base, _Derived)
|                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:462:5: note: candidate: 'template  requires  contiguous_iterator std::span(_Iter, _End)-> span'
462 |     span(_Iter, _End)
|     ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:462:5: note:   candidate expects 2 arguments, 1 provided
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:458:5: note: candidate: 'template std::span(const array&)-> span'
458 |     span(const array&)
|     ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:458:5: note:   template argument deduction/substitution failed:
:13:43: note:   'std::vector' is not derived from 'const std::array'
13 |     auto vec_bool_view = std::span(boolvec);
|                                           ^
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:455:5: note: candidate: 'template std::span(array&)-> span'
455 |     span(array&) -> span;
|     ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:455:5: note:   template argument deduction/substitution failed:
:13:43: note:   'std::vector' is not derived from 'std::array'
13 |     auto vec_bool_view = std::span(boolvec);
|                                           ^
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:452:5: note: candidate: 'template std::span(_Type (&)[_ArrayExtent])-> span'
452 |     span(_Type(&)[_ArrayExtent]) -> span;
|     ^~~~
/opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/span:452:5: note:   template argument deduction/substitution failed:
:13:43: note:   mismatched types '_Type [_ArrayExtent]' and 'std::vector'
13 |     auto vec_bool_view = std::span(boolvec);
|                                           ^
Каковы мои варианты? Просто используйте std::vector? Я почти подумал, что наконец-то смогу использовать эту векторную оптимизацию.. :/

Подробнее здесь: https://stackoverflow.com/questions/792 ... nt-compile
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»