TeenyTinySparkles
TeenyTinySparkles

Reputation: 419

Convert a nullptr to std::span

I have a const z* zs = nullptr;

I want to convert zs to std::span

When I try to do std::span<const z>(zs) I get an error saying

error: no matching function for call to ‘std::span::span(const z* const&)’

How do I convert to zs to std::span

I tried std::span<const z>(zs[0]) it seems to compile. Is that way correct?

Upvotes: 5

Views: 1941

Answers (2)

Mestkon
Mestkon

Reputation: 4061

std::span has a constructor which takes a pointer and a size, use that:

std::span<const z>(zs, 0)

Upvotes: 6

Caleth
Caleth

Reputation: 62894

You need to provide a length to go along with your pointer. A null pointer points to 0 elements. Are you reassigning sz somewhere?

An empty span is constructed from no arguments.

std::span<const z>{}

Upvotes: 2

Related Questions