Equivalent of python walrus operator (:=) in c++11?

Viewed 1320

Recently I have been using the := operator in python quite a bit, in this way:

if my_object := SomeClass.function_that_returns_object():
    # do something with this object if it exists
    print(my_object.some_attribute)

The question

Is there any way to do this in c++11 without the use of stdlib? for example in an arduino sketch if I wanted to use a method that may potentially return zero, such as:

if(char * data = myFile.readBytes(data, dataLen))
{
    // do something
}
else
{
    // do something else
}
3 Answers

Python's := assignment expression operator (aka, the "walrus" operator) returns the value of an assignment.

C++'s = assignment operator (both copy assignment and move assignment, as well as other assignment operators) does essentially the same thing, but in a different way. The result of an assignment is a reference to the object that was assigned to, allowing that object to be evaluated in further expressions.

So, the equivalent of:

if my_object := SomeClass.function_that_returns_object():
    # do something with this object if it exists
    print(my_object.some_attribute)

Would be just like you showed:

SomeType *object;
if ((my_object = SomeClass.function_that_returns_object())) {
    // do something with this object if it exists
    print(my_object->some_attribute);
}

If function_that_returns_object() returns a null pointer, the if evaluates object as false, otherwise it evaluates as true. The same can be done with other types, eg:

int value;
if ((value = SomeClass.function_that_returns_int()) == 12345) {
    // do something with this value if it matches
}

Not exactly, no.

As is mentioned in other answers, the c++ = operator already does most of what you want. If have an existing variable then assignment to that variable returns a reference to it, so you can put that into an if condition:

Foo* a_pointer;
if (a_pointer = some_function()) {
    //...
}

Here, the body of the if conditional will execute if some_function return a non-null pointer and a_pointer will be a copy of the pointer returned by some_function.

Unlike the walrus operator though, this has the limitation that a_pointer had to first be defined outside of the if condition.


C++17 adds something a bit closer, in that you can initialize a variable inside of the if condition with a special if-initializer syntax:

if (Foo* a_pointer = some_function(); a_pointer) {
    //...
}

Note that the initializer still doesn't directly contribute to the truthiness of the if condition. It's only the expression after the ; that determines if the body of the if statement will execute. In this case, a_pointer is initialized to be the value returned by some_function in the initializer and then the condition part checks if a_pointer is truthy.

According to the documentation, readBytes returns the number of bytes placed in the buffer (not a pointer), so I think you just need to do something like:

if(myFile.readBytes(data, dataLen))
{
    // do something with data
}
else
{
    // do something else
}
Related