template argument deduction compile error with boost's `static_vector`

huangapple go评论71阅读模式
英文:

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 &lt;span&gt;
#include &lt;boost/container/static_vector.hpp&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

int main(){
    boost::container::static_vector&lt;int, 10&gt; x{1,2,3};
    std::vector y{1,2,3};
    for(const auto&amp; i : std::span(y.begin()+1, y.end()))
        std::cout &lt;&lt; i &lt;&lt; &#39;\n&#39;;
}

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: &#39;template&lt;class _Type, long unsigned int _Extent, class _It, class _End&gt;  requires (contiguous_iterator&lt;_It&gt;) &amp;&amp; (sized_sentinel_for&lt;_End, _It&gt;) &amp;&amp; ((std::span&lt;_Type, _Extent&gt;::__is_compatible_ref::value) &amp;&amp; !(is_convertible_v&lt;_End, std::size_t&gt;)) span(_It, _End)-&gt; std::span&lt;_Type, _Extent&gt;&#39;
  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&amp; i : x | std::views::drop(1))
        std::cout &lt;&lt; i &lt;&lt; &#39;\n&#39;;

https://godbolt.org/z/3rEE8sPr7

huangapple
  • 本文由 发表于 2023年4月19日 21:29:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055122.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定