Why does using parentheses with a standard constructor result in a variable?

After watching the Louis Brandy interview at CppCon 2017, I was shocked to find that this code really compiles:

#include <string>

int main() {

    std::string(foo);

    return 0;
}    

And for some reason std::string(foo)it is identical std::string foo, that is, a variable declaration. I find this completely contradictory and I see no reason for C ++ to work this way. I expect this to give an error in an undefined identifier foo.

In fact, type expressions token1(token2)have even more possible interpretations than I previously thought.

So my question is: what is the reason for this horror? When is this rule really necessary?

PS Sorry for the poorly worded title, please feel free to change it!

+6
1

, , [stmt.ambig]:

, - : - , (. .

, , [dcl.ambig.res]:

, - , [stmt.ambig], . - . , , [stmt.ambig], , .

:

oh std::string("foo") std::string(foo)

. . , , -.

, , , (, . , , , , , , , .

+14

All Articles