gpt4 book ai didi

c++ - C/C++中不带括号的else语句的多行格式

转载 作者:行者123 更新时间:2023-12-02 09:56:42 26 4
gpt4 key购买 nike

我正在重写一些C++代码,我有以下内容

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.; });

我认为这相当于
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.; });

但是有一点儿担心,编译器将原始解释为
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.; });
}

我在任何地方都找不到对VSVC编译器做什么的引用。哪有

最佳答案

相信您对代码的假设是正确的。

对于大多数语言,我只知道在该条件下执行一条语句或复合语句(例如大括号)。


ThreadPool::Parallel_for(0, normalInfo->size(), [&](unsigned int, size_t i) { (*normalInfo)[i] *= (Real)-1.; });

该行在条件之后真的很奇怪地隐藏了,我认为这是一种代码味道,因为它使代码难以阅读,理解和调试。

编辑,因为我喜欢完整的答案:同样,您实际上不需要知道VS C++编译器在这里将要执行的操作,因为它已定义。有关此信息,请参见 Alex Allain's post

也可以引用MSDN文档: https://docs.microsoft.com/en-US/cpp/cpp/statements-cpp?view=vs-2019

https://docs.microsoft.com/en-US/cpp/cpp/if-else-statement-cpp?view=vs-2019
状态

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 ...



要了解这一点,请检查 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 ] ;


这是 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 ] }


结论:您的假设是正确的,您的恐惧没有根据:)

关于c++ - C/C++中不带括号的else语句的多行格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59519719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com