在控制结构中定义变量

发布于 2024-12-06 11:05:48 字数 593 浏览 0 评论 0原文

根据该标准,在控制结构中声明变量与在其他地方声明变量之间的行为有什么区别?我似乎找不到任何提及它的地方。

如果我指的不清楚,这里有一个示例:

if (std::shared_ptr<Object> obj = objWeakPtr.lock())

如您所见,我在 if 块中声明并初始化一个局部变量 obj

另外,是否有任何技术原因说明为什么此语法在代替条件使用时没有给出任何特殊行为?例如,添加一组额外的括号会导致编译器错误;这也可以防止变量与其他条件链接。

// Extra brackets, won't compile.
if ((std::shared_ptr<Object> obj = objWeakPtr.lock()))

// If the above were valid, something like this could be desirable.
if ((std::shared_ptr<Object> obj = objWeakPtr.lock()) && obj->someCondition())

According to the standard, what is the difference in behavior between declaring variables in control structures versus declaring variables elsewhere? I can't seem to find any mention of it.

If what I'm referring to isn't clear, here's an example:

if (std::shared_ptr<Object> obj = objWeakPtr.lock())

As you can see, I'm declaring and initializing a local variable, obj, in the if block.

Also, is there any technical reason as to why this syntax isn't given any special behavior when used in place of a conditional? For example, adding an additional set of brackets results in a compiler error; this also prevents the variable from being chained with other conditions.

// Extra brackets, won't compile.
if ((std::shared_ptr<Object> obj = objWeakPtr.lock()))

// If the above were valid, something like this could be desirable.
if ((std::shared_ptr<Object> obj = objWeakPtr.lock()) && obj->someCondition())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸢与 2024-12-13 11:05:48

根据标准,在控制结构中声明变量与在其他地方声明变量之间的行为有什么区别?我似乎找不到任何提及它的地方。

控制结构引入内的声明与其他地方的声明没有什么不同。这就是为什么你找不到任何差异。

6.4/3 确实为此描述了一些特定的语义,但并不令人意外:

[n3290: 6.4/3]: 由条件中的声明引入的名称
(由 type-specifier-seq 或声明符引入
条件)的范围是从其声明点到结束
由条件控制的子语句。如果名字是
在由 控制的子语句的最外层块中重新声明
条件,重新声明名称的声明格式不正确。 [..]


此外,是否有任何技术原因可以解释为什么此语法在代替条件使用时没有给出任何特殊行为?例如,添加一组额外的括号会导致编译器错误;这也可以防止变量与其他条件链接。

if 条件可以包含声明性语句表达式。任何表达式都不能包含声明性语句,因此也不能混合它们。

[n3290: 6.4/1]: 选择语句选择多个控制流之一。

选择语句:
    if(条件)语句
    if ( 条件 ) 语句 else 语句
    switch(条件)语句
健康)状况:
    表达
    属性说明符-seq[opt] decl-说明符-seq 声明符 = 初始化子句
    attribute-specifier-seq[opt] decl-specifier-seq 声明符花括号初始化列表

这一切都源于语法产生。

According to the standard, what is the difference in behavior between declaring variables in control structures versus declaring variables elsewhere? I can't seem to find any mention of it.

Declarations inside control structure introductions are no different that declarations elsewhere. That's why you can't find any differences.

6.4/3 does describe some specific semantics for this, but there are no surprises:

[n3290: 6.4/3]: A name introduced by a declaration in a condition
(either introduced by the type-specifier-seq or the declarator of the
condition) is in scope from its point of declaration until the end of
the substatements controlled by the condition. If the name is
re-declared in the outermost block of a substatement controlled by the
condition, the declaration that re-declares the name is ill-formed. [..]


Also, is there any technical reason as to why this syntax isn't given any special behavior when used in place of a conditional? For example, adding an additional set of brackets results in a compiler error; this also prevents the variable from being chained with other conditions.

An if condition can contain either a declarative statement or an expression. No expression may contain a declarative statement, so you can't mix them either.

[n3290: 6.4/1]: Selection statements choose one of several flows of control.

selection-statement:
    if ( condition ) statement
    if ( condition ) statement else statement
    switch ( condition ) statement
condition:
    expression
    attribute-specifier-seq[opt] decl-specifier-seq declarator = initializer-clause
    attribute-specifier-seq[opt] decl-specifier-seq declarator braced-init-list

It all just follows from the grammar productions.

厌味 2024-12-13 11:05:48

在条件中声明和初始化变量与在其他地方声明变量的区别在于,变量用作条件,并且位于 if 条件语句内的范围内,但超出该条件之外的范围。另外,在 if 条件内重新声明变量是不合法的。所以

bool x=something();
if(x) {
    bool y=x; // legal, x in scope
    int x=3; // legal
    ...
}
while (x=something_else()) // legal, x still in scope
...

但是:

if(bool x=something()) 
    bool y=x; // still legal
    int x=3; // not legal to redeclare
    ...
}
while (x=something_else()) // x declared in condition not in scope any more

The difference from declaring and initializing a variable in the condition and declaring it elsewhere is that the variable is used as the condition, and is in scope inside the if's conditional statement, but out of scope outside that condition. Also, it's not legal to re-declare the variable inside the if condition. So

bool x=something();
if(x) {
    bool y=x; // legal, x in scope
    int x=3; // legal
    ...
}
while (x=something_else()) // legal, x still in scope
...

but:

if(bool x=something()) 
    bool y=x; // still legal
    int x=3; // not legal to redeclare
    ...
}
while (x=something_else()) // x declared in condition not in scope any more
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文