The most unpleasant parsing: why not `g ((f ()));` call` f` default constructor and pass the result to `g` ctor, which takes` f`?

This is not a duplicate. The most unpleasant parsing: why A a (()); work? , which is based on parsing in a form A a(());, the thinking of which will be able to construct an object by default Ausing an additional set of parentheses.

On the contrary, my question concerns two classes: fand g, where fhas a default constructor, and gctor accepts f. I want to call gctor with a temporary argument f, without using a single initialization syntax. There std::coutin gctor, so the lack of output means declaring a function instead of an gobject. I commented on the sample code with 3 numbers in the comments. # 1 and # 2 were compiled C # 3 commented out, and vice versa:

#include <iostream>
struct f {};
struct g {
    g(f) { std::cout << "g ctor\n"; }
};
int main() {
                          // -----Output-----
    g( f() );             // #1: function declaration; expected
    g( ( f() ) );         // #2: also a function declaration; UNEXPECTED
    // g myG( ( f() ) );  // #3: "g ctor" ONLY if #1 & #2 are commented out;
                          //     ^ ... linker error otherwise
}

# 1: I thought that # 1 declares an anonymous function that returns gand takes a pointer to a function that takes 0 arguments and returns f. I'm wrong?

# 2: , , # 2 , f ctor. . ?

# 3: - # 2, # 3 myG. # 3 , # 1 # 2 . VC12:

error LNK2019: unresolved external symbol "struct g __cdecl f(void)" (?f@@YA?AUg@@XZ) referenced in function _main

fatal error LNK1120: 1 unresolved externals.

g++ 4.8: undefined reference to 'f()'

?

# 3 ?

?

+4
1

f, g.

     g( f() );
  //  ^     ^  redundant set of parentheses

- , (, , ). . , , , :

// function taking an int and returning
// a pointer to a function that takes a char
// and returns a g
g ( *f(int) )(char);
//^         ^ needed, syntax error without them

:

# 1 # 2, f main, g myG( ( f() ) ); g myG . , f.

# 1 # 2 , f , :

g myG( ( f() ) )
//     ^     ^   these force an expression

.

, :

   ( g(f()) );
// ^        ^  must be an expression as declarations can't be parenthesized

- Lisp -y: static_cast<g>(f());

+7

All Articles