c++stdstd-span

What's the purpose of std::dynamic_extent in std::span


I know std::span is static. It is just view over bunch of vector/array/etc. elements.

I see constructors of span, and it seems like std::dynamic_extent is used in 4-6. But in those constructors, there is a required template parameter for the size - std::size_t N. To me this means that the size/count/len is known at compile time. So what really is std::dynamic_extent?


Solution

  • The definition of std::dynamic_extent is

    inline constexpr std::size_t dynamic_extent 
        = std::numeric_limits<std::size_t>::max();
    

    It's a special value of a std::size_t that's used to indicate that the std::span has a dynamic extent.

    There is a required template argument for the size - std::size_t N. To me this means that the size/count/len is known at compile time.

    The "size" of the std::span is still specified at compile time, it's just that when the "size" takes on that special value, it's treated as a dynamic extent.