Reputation: 23831
I am rewriting some C++ code, I have the following
if (ConfidenceBias.value > 0) *normalInfo = tree.setDataField(NormalSigs(), *samples, *sampleData, density, pointWeightSum, ConversionAndBiasFunction);
else *normalInfo = tree.setDataField(NormalSigs(), *samples, *sampleData, density, pointWeightSum, ConversionFunction); ThreadPool::Parallel_for(0, normalInfo->size(), [&](unsigned int, size_t i) { (*normalInfo)[i] *= (Real)-1.; });
I assume that this is equivalent to
if (ConfidenceBias.value > 0)
{
*normalInfo = tree.setDataField(NormalSigs(), *samples, *sampleData, density, pointWeightSum, ConversionAndBiasFunction);
}
else
{
*normalInfo = tree.setDataField(NormalSigs(), *samples, *sampleData, density, pointWeightSum, ConversionFunction);
}
ThreadPool::Parallel_for(0, normalInfo->size(), [&](unsigned int, size_t i) { (*normalInfo)[i] *= (Real)-1.; });
but there is a slight conern that the compiler is interpreting the original as
if (ConfidenceBias.value > 0)
{
*normalInfo = tree.setDataField(NormalSigs(), *samples, *sampleData, density, pointWeightSum, ConversionAndBiasFunction);
}
else
{
*normalInfo = tree.setDataField(NormalSigs(), *samples, *sampleData, density, pointWeightSum, ConversionFunction);
ThreadPool::Parallel_for(0, normalInfo->size(), [&](unsigned int, size_t i) { (*normalInfo)[i] *= (Real)-1.; });
}
I cannot find a reference to what the VSVC compiler does anywhere. Which is it?
Upvotes: 0
Views: 86
Reputation: 6490
AFAIK your assumption about the code is correct.
For most languages I am ware of only one statement or a compound-statement (e.g. braces) will be executed on the condition.
The
ThreadPool::Parallel_for(0, normalInfo->size(), [&](unsigned int, size_t i) { (*normalInfo)[i] *= (Real)-1.; });
line is really really oddly hidden after the condition and I would assume this a code smell because it makes code hard to read, understand and debug.
Edit because I like complete answers: Also you actually do not need to know what VS C++ compiler is going to do here because this is defined. See Alex Allain's post about this.
Refer to the MSDN docs for this as well: https://learn.microsoft.com/en-US/cpp/cpp/statements-cpp?view=vs-2019
https://learn.microsoft.com/en-US/cpp/cpp/if-else-statement-cpp?view=vs-2019 states
Controls conditional branching. Statements in the if-block are executed only if the if-expression evaluates to a non-zero value (or TRUE). If the value of expression is nonzero, statement1 and any other statements in the block are executed and the else-block, if present, is skipped. If the value of expression is zero, then the if-block is skipped and the else-block, if present, is executed. Expressions that ...
To understand this check the definition of statement:
Expression statements cause expressions to be evaluated. No transfer of control or iteration takes place as a result of an expression statement.
The syntax for the expression statement is simply Syntax
[expression ] ;
and here is the compound statement block definition
A compound statement consists of zero or more statements enclosed in curly braces ({ }). A compound statement can be used anywhere a statement is expected. Compound statements are commonly called "blocks." Syntax
{ [ statement-list ] }
Verdict: Your assumption was valid, your fear unfounded :)
Upvotes: 3