1
struct A{
   A(){}
};

struct B{
    B(const A& a){}
};

int main()
{
 //Originally I would like to write down below code

A a;  
B b(a); 

//Unfortunately I end up with below code by accident and there is no compile error
//I have figured out the below does not create temporary A and call B constructor to 
//create B as the above codes,
//it declares a function with return value B, name b, 
//and some input parameter, my question is  1) what the input parameter is ? 
//2) How to implement such a function.

B b(A());   // There is no global function A() in my test case.

}

The question is in the comment, I hope some people can help me to understand it. Thank you very much.

Zhongkun Ma
  • 421
  • 2
  • 9

2 Answers2

3

It declares a function named b that returns B, which has a single parameter of type A (*)(), that is, pointer to function taking no arguments and returning A. The declarator A() means "function taking no arguments and returning A", but whenever you declare a parameter to have function type, it's rewritten to become a pointer to function. The parameter in this declaration is unnamed (you don't have to specify a name for a parameter if you don't want to).

To implement such a function you would need a definition, e.g.,

B b(A a()) {
    // do something with "a"
    // note: the type of "a" is still pointer to function
}

See, e.g., Is there any use for local function declarations?

Community
  • 1
  • 1
Brian Bi
  • 111,498
  • 10
  • 176
  • 312
  • Thank you very much for your help.I get the point. One more question is why A() can be expanded to A(*)(). I do not get the point. – Zhongkun Ma Oct 20 '14 at 21:50
  • @ZhongkunMa `A()` means function returning `A` (with no parameters), and `A(*)()` means pointer to function returning `A` (again with no parameters). It's just a rule that if you try to declare a function one of whose parameters is a function, the parameter's type is adjusted to be the corresponding function pointer type instead. – Brian Bi Oct 20 '14 at 21:55
  • Thank you again for your fast-response. I had tested one thing, if i declare the "B b" function as `B b(A(a)(),A(*b)(),A(*)(),A());` The "Return A" function as `A returnA() {return A()};` What is reason I can call b function as `b(returnA,returnA,returnA,returnA,)` It seems `b(A(a)(),A(*b)(),A(*)(),A())` are all identical, aren't I right ? I am looking forward to your reply. – Zhongkun Ma Oct 20 '14 at 22:06
  • @ZhongkunMa I don't understand what you're asking. Maybe you should post a new question and explain in more detail what your new question is. – Brian Bi Oct 20 '14 at 22:12
  • Thank you very much for your support. I have found the answers to my question. I had learnt a lot from this. – Zhongkun Ma Oct 21 '14 at 09:06
  • @ZhongkunMa If you found this answer helpful, consider accepting it (click the checkmark next to this answer) – Brian Bi Oct 30 '14 at 04:47
2

B b(A()) declares a function named b which returns a B and takes a function pointer as an argument. The function pointer points to a function which returns a A and takes no arguments.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • What is the name of the function ? and how to realize this function? Thanks – Zhongkun Ma Oct 20 '14 at 21:38
  • @ZhongkunMa Since this is a **declaration** for the function `b`, no parameter names are required and none is given here. If you want to implement the function, then you will be required to provide a name with the function **definition**. – Code-Apprentice Oct 20 '14 at 21:39