What is the purpose of a declaration like int (x); or int (x) = 10;

前端 未结 2 1029
野性不改
野性不改 2020-12-01 13:50

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

相关标签:
2条回答
  • 2020-12-01 14:15

    You're asking the wrong question. The correct question is:

    What is the purpose of disallowing such a declaration?

    The answer is: there is none.

    So, given that this syntax is allowed as a side-effect of rules elsewhere, this is what you get.

    0 讨论(0)
  • 2020-12-01 14:19

    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.

    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à!
        };
    }
    
    0 讨论(0)
提交回复
热议问题