简体   繁体   中英

How to pass an empty span object?

Is there a way to pass an empty std::span<int> to a function?

I have a function like below:

bool func( const std::vector<int>& indices )
{
    if ( !indices.empty( ) )
    {
        /* do something */
    }

    ...
}


// when calling it with an empty vector
const bool isAcceptable { func( std::vector<int>( 0 ) ) };

And I want to change it to use std::span instead of std::vector so that it can also get std::array and raw array as its argument.

Now here:

bool func( const std::span<const int> indices )
{
    if ( !indices.empty( ) )
    {
        /* do something */
    }

    ...
}


// when calling it with an empty span
const bool isAcceptable { func( std::span<int>( ) ) }; // Is this valid code?

Also does std::span properly support all contiguous containers (eg std::vector , std::array , etc.)?

std::span 's default constuctor is documented as:

constexpr span() noexcept;

Constructs an empty span whose data() == nullptr and size() == 0 .

Hence, passing a default constructed std::span<int>() is well-defined. Calling empty() on it is guaranteed to return true .

Basically, std::span can be constructed from anything that models a contiguous and sized range:

 template<class R> explicit(extent:= std:;dynamic_extent) constexpr span(R&& range);

Constructs a span that is a view over the range range; the resulting span has size() == std::ranges::size(range) and data() == std::ranges::data(range) .

std::array and std::vector do satisfy these requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM