gk017
gk017

Reputation: 87

Reuse of variable of type auto

I have a special class to be used as return type of methods, containing the wanted value or in case of failure an error message which is even cascading from earlier errors. It works as expected.

As the returned type is differently complex I like to use the keyword auto. But when using a lot of methods I have to create new return variables.

A typical part of code looks like this:

auto ret1 = methodA();
if(ret1.isValid()...

auto ret2 = methodB();
if(ret2.isValid()...

I dont like to always create a new variable. But I like the elegant way of error handling. Using a more dump return type like an error code in integer would solve the problem but then I have no benefit from the error handling return type.

Is there any trick to reuse the first return variable ret1?

Upvotes: 3

Views: 850

Answers (4)

Dmitry Kuzminov
Dmitry Kuzminov

Reputation: 6584

I dont like to always create a new variable.

Much better is to create a const variable:

const auto ret1 = methodA();
if(ret1.isValid()...

const auto ret2 = methodB();
if(ret2.isValid()...

In this case you need to make const all the methods like isValid, but that is even better: "is" shouldn't have side effects and modify the state.

Next step is to remove the temp variable at all:

if(methodA().isValid()) {
    ...
}

if(methodB().isValid()) {
    ...
}

The alternative is to wrap each function call into a block:

{
    const auto ret = methodA();
    if(ret.isValid()...
}

{
    const auto ret = methodB();
    if(ret.isValid()...
}

This allows you to reuse the const variable name.

Each block becomes a candidate for extraction into a separate function (see Uncle Bob in "Clean Code").

Upvotes: 1

Jesper Juhl
Jesper Juhl

Reputation: 31459

auto is not a type. In auto foo = bar(); the compiler simply figures out what type bar() actually returns and substitutes that in. So if bar() returns int then that's the type of foo, if it returns bool then that is the type of foo. And once the type that auto should be replaced with (the first time) has been determined, then it can never change. auto doesn't mean "variable type" it just means "hey compiler, I'm too lazy to figure out the type to put here, please do it for me", but there is no difference what-so-ever compared to you just writing the final type yourself. So, you can reuse the variable if what you assign to it the second time is of the same type as the first time - otherwise not.

Upvotes: 2

Asteroids With Wings
Asteroids With Wings

Reputation: 17454

auto is not a type.

It is a keyword, that says "put the type here for me, by deducing it from the initial value". That occurs during compilation, once.

You cannot reuse ret1 to store an object of a different type, whether you use auto or not.

This shouldn't really be a problem. If you're concerned about "running out of names", or "having many similar names", your names are not descriptive enough and/or your scopes aren't tight enough.

Upvotes: 3

Artyer
Artyer

Reputation: 40891

You would have to create new scopes to reuse the variable name for a different variable, like:

{
    auto ret = methodA();
    if (ret.isValid()) ...
}
{
    auto ret = methodB();
    if (ret.isValid()) ...
}

You can also take advantage of the scope created by if, placing the init-statement inside:

if (auto ret = methodA(); ret.isValid()) ...

if (auto ret = methodB(); ret.isValid()) ...

Upvotes: 4

Related Questions