Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for declaring variable without initialising it, so auto is unavailable

Tags:

c++

c++11

I want to declare two variables of the same type, and have the compiler figure out the types. However I don't want to initialise one of the variables until later. I don't think I can use auto here, so what's the best option?

std::vector<int> v;

// `start` and `end` should be the same type
auto start = v.begin();
??? end;

// complicated code to assign a value to `end` (i.e. putting
// the code in a function or using ?: is not practical here)  
if (...) {
    end = ...;
} else {
    end = v.end();
}

What's the best way to tell the compiler that end should be the same type as start, but without having to initialise the variable?

auto start = v.begin(), end;  // Doesn't work, `end` has to be initialised
decltype(start) end;          // Does work, but not sure if it's best practice

Update

A couple of comments have suggested ways that would work in certain situations, so I am clarifying my situation here:

std::vector<int> v;
int amount = 123;

// `start` and `end` should be the same type
auto start = v.begin();
??? end;

// code to assign a value to `end`
if (amount) {
    end = start + amount;
    amount = 0;
} else {
    end = v.end();
}

I believe a lambda function would be trickier here, because amount is being reset to 0 after end is calculated, so in a lambda function that calculates a value for end, amount = 0 would have to come after the return statement. The only option would be to create more local variables, which would incur an (admittedly tiny) performance penalty.

like image 856
Malvineous Avatar asked Feb 11 '15 10:02

Malvineous


People also ask

Can you declare a variable without initializing it?

Declaring a Variable Without Initializing We can use the let keyword. We use the let keyword when variables are mutable. That means we can change or set the value later on in the program.

What happens if you don't initialize a variable?

An uninitialized variable is a variable that has not been given a value by the program (generally through initialization or assignment). Using the value stored in an uninitialized variable will result in undefined behavior.

Do you need to initialize a variable before it will work?

When you declare a variable, you should also initialize it. Two types of variable initialization exist: explicit and implicit. Variables are explicitly initialized if they are assigned a value in the declaration statement.

Can we use a variable even if it is not initialized?

Examples of $null Anytime you try to use a variable that you have not initialized, the value is $null . This is one of the most common ways that $null values sneak into your code. If you happen to mistype a variable name then PowerShell sees it as a different variable and the value is $null .


1 Answers

My personal approach would be to call a lambda in-place:

std::vector<int> v;

///////////////////

auto start = v.begin(), end = [&]{
    if (...) {
        // complicated code to compute a value for `end`
    }
    else
        return v.end();
}();

If automatic return type deduction for the lambda fails for any reason (e.g. there are multiple return statements), just replace [&]{ with [&]() -> decltype(start) {.


Edit:

std::vector<int> v;
int amount = 123;

///////////////////

auto start = v.begin(), end = [&]{
    auto ret = v.end();
    if (amount) {
        ret = start + amount;
        amount = 0;
    }
    return ret;
}();
like image 85
ildjarn Avatar answered Sep 24 '22 02:09

ildjarn