声明的目的是什么,如int(x);或int(x)= 10; [英] What is the purpose of a declaration like int (x); or int (x) = 10;

查看:229
本文介绍了声明的目的是什么,如int(x);或int(x)= 10;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你看看§8/ 4 中 * declarator * s的语法,你会注意到一个 noptr声明符 可写为( ptr-declarator ), declarator-id ),它验证标题中的声明。事实上这个代码编译没有问题:

If you look at the grammar for *declarator*s in §8/4 you'll notice that a noptr-declarator can be written as (ptr-declarator), that is, it can be written as (declarator-id), which validates declarations like the ones in the title. As matter of fact this code compiles without a problem:

#include <iostream>
struct A{ int i;};
int (x) = 100;
A (a) = {2};
int main()
{
    std::cout << x << '\n';
    std::cout << a.i << '\n';
}

允许这些括号的目的是什么

推荐答案

事实上,这个规则是适用于您的情况不是故意的:它最终保持语法简单的结果。没有激励来禁止你的声明,但有很大的阻碍,使规则复杂化,特别是如果那些是复杂的,因为他们是。

The fact that this rule is applicable in your case is not deliberate: It's ultimately a result of keeping the grammar simple. There is no incentive to prohibit declarations such as yours, but there are great disincentives to complicate rules, especially if those are intricate as they are.

总之,如果你不想使用这种不必要的混淆语法。

C ++很少强迫你写可读的代码。

In short, if you don't want to use this needlessly obfuscated syntax, don't.
C++ rarely forces you to write readable code.

令人惊讶的是,括号可以保存日期,但:

Surprisingly there are scenarios in which parentheses can save the day, though:

std::string foo();

namespace detail
{
    int foo(long); // Another foo

    struct Bar
    {
        friend std::string ::foo(); // Doesn't compile for obvious reasons.

        friend std::string (::foo)(); // Voilà!
    };
}

这篇关于声明的目的是什么,如int(x);或int(x)= 10;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆