Search code examples
c++templatesboost

template argument deduction compile error with boost's `static_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!


Solution

  • 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.


    Offtopic I would rewrite your code this way:

        for(const auto& i : x | std::views::drop(1))
            std::cout << i << '\n';
    

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