Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C++17 can an if statement with an initializer be used to unpack an optional?

I'm writing some code using std::optional's and am wondering if C++17's 'if statements with initializers' will be able to help unpack values?

std::optional<int> optionalInt = GetOptionalInt();

I'm making up the function Unpack here:

if( auto [value, has_value] = optionalInt.Unpack(); has_value )
{
    // Use value here.
}

But, my question is. Will C++17 'if statement with initializer' help here? If so, how would it be coded?

Update, this is actually mainly an issue when using optional which is extremely easy to misuse because the optional and *optional both return bools and you don't get any compiler warning when somebody trys to access the value and forgets the *.

like image 643
Scott Langham Avatar asked Sep 20 '16 16:09

Scott Langham


People also ask

Can we initialize in if condition?

C++17 If statement with initializer Now it is possible to provide initial condition within if statement itself. This new syntax is called "if statement with initializer". This enhancement simplifies common code patterns and helps users keep scopes tight.

What is std :: optional used for?

The class template std::optional manages an optional contained value, i.e. a value that may or may not be present. A common use case for optional is the return value of a function that may fail.

Does STD optional allocate?

What's more, std::optional doesn't need to allocate any memory on the free store. std::optional is a part of C++ vocabulary types along with std::any , std::variant and std::string_view .

What's new in C++17 if statement with initializer?

C++17 has extended existing if statement’s syntax. Now it is possible to provide initial condition within if statement itself. This new syntax is called "if statement with initializer". This enhancement simplifies common code patterns and helps users keep scopes tight.

What is an IF statement with initializer?

This new syntax is called "if statement with initializer". This enhancement simplifies common code patterns and helps users keep scopes tight. Which in turn avoids variable leaking outside the scope.

How to use init statement in IF-ELSE block in C++?

In C++17 the init statement is called an initializer, and we can directly put it into the if-else block as follows. if (init-statement; condition) { // Do Something } else { // Do Something else }. The scope of the conditioned variable gets limited to the current if-else block.

How do you initialize a variable in C++ if-else?

In C++17 the init statement is called an initializer, and we can directly put it into the if-else block as follows if (init-statement; condition) { // Do Something } else { // Do Something else } The scope of the conditioned variable gets limited to the current if-else block.


1 Answers

There is not, and cannot possibly be, such an Unpack() function.

But you could certainly do:

if (std::optional<int> o = GetOptionalInt(); o) {
    // use *o here
}

though the extra o check is kind of redundant.


This is one of those places where it'd be nice if optional<T> modeled a container of at most one element, so that you could do:

for (int value : GetOptionalInt()) {
    // possibly not entered
}

but we don't have that interface.

like image 130
Barry Avatar answered Sep 22 '22 15:09

Barry