英文:
template argument deduction compile error with boost's `static_vector`
问题
以下是您要翻译的代码部分:
#include <span>
#include <boost/container/static_vector.hpp>
#include <iostream>
#include <vector>
int main(){
boost::container::static_vector<int, 10> x{1,2,3};
std::vector y{1,2,3};
for(const auto& i : std::span(y.begin()+1, y.end()))
std::cout << i << '\n';
}
但如果我尝试在范围循环中使用 x
替换 y
,编译将会失败:https://godbolt.org/z/ahWPv7rfe
我认为这是与 static_vector
相关的小错误,因为上述代码在使用 vector
时可以正常工作... 但是,我该如何修改代码以解决编译器错误呢?谢谢!
英文:
The following piece of code works fine:
#include <span>
#include <boost/container/static_vector.hpp>
#include <iostream>
#include <vector>
int main(){
boost::container::static_vector<int, 10> x{1,2,3};
std::vector y{1,2,3};
for(const auto& i : std::span(y.begin()+1, y.end()))
std::cout << i << '\n';
}
but will fail at compile if i try to use x
in place of y
in the ranged for loop: https://godbolt.org/z/ahWPv7rfe
I think this is a minor bug with static_vector
as the above works fine with vector
... Still, what can I do with my code to resolve the compiler error? Thanks!
答案1
得分: 2
以下是要翻译的内容:
这里是最重要的错误行:
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/span:170:9: 注意: 候选项:'template<class _Type, long unsigned int _Extent, class _It, class _End> requires (contiguous_iterator<_It>) && (sized_sentinel_for<_End, _It>) && ((std::span<_Type, _Extent>::__is_compatible_ref::value) && !(is_convertible_v<_End, std::size_t>)) span(_It, _End)->std::span<_Type, _Extent>'
170 | span(_It __first, _End __last)
正如您所看到的,在这个版本的boost中未满足contiguous_iterator概念。
基本上,对于此版本,未引入对C++20的boost支持。
它从boost 1.77开始起作用:https://godbolt.org/z/cfMxTrGsn
因此,您需要更新boost库。
此外,我会这样重写您的代码:
for(const auto& i : x | std::views::drop(1))
std::cout << i << '\n';
https://godbolt.org/z/3rEE8sPr7
英文:
Here is most important error line:
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/span:170:9: note: candidate: 'template<class _Type, long unsigned int _Extent, class _It, class _End> requires (contiguous_iterator<_It>) && (sized_sentinel_for<_End, _It>) && ((std::span<_Type, _Extent>::__is_compatible_ref::value) && !(is_convertible_v<_End, std::size_t>)) span(_It, _End)-> std::span<_Type, _Extent>'
170 | span(_It __first, _End __last)
As you can see contiguous_iterator concept is not meet in this version of boost.
Basically boost support for C++20 was not introduced for this version.
It works since boost 1.77: https://godbolt.org/z/cfMxTrGsn
So you need to update boost library.
<hr>
Offtopic I would rewrite your code this way:
for(const auto& i : x | std::views::drop(1))
std::cout << i << '\n';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论