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.
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.
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.
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.
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 .
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;
}();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With