Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous declarations

What is the difference between the following two declarations:

 1. int foo(int); 
 2. int foo(int());

I am not sure if both the declarations are equivalent. What makes (2) different from (1)?

like image 559
Ajay Somani Avatar asked Oct 04 '10 14:10

Ajay Somani


People also ask

How do you fix an ambiguous error in C++?

You can resolve ambiguity by qualifying a member with its class name using the scope resolution ( :: ) operator. The statement dptr->j = 10 is ambiguous because the name j appears both in B1 and B2 .

What does it mean when something is ambiguous C++?

Access to a base class member is ambiguous if you use a name or qualified name that does not refer to a unique function or object. The declaration of a member with an ambiguous name in a derived class is not an error. The ambiguity is only flagged as an error if you use the ambiguous member name.


1 Answers

int foo(int); is the declaration of a function taking an integer as an argument and returning an integer as well

int foo(int()); declares a function taking as an argument "a pointer to a function returning int and taking {no arguments[in C++] and unspecified number of arguments[in C]} " and returning an integer.

(2) is equivalent to int foo(int (*pf)()) and int foo(int f())

like image 79
Prasoon Saurav Avatar answered Oct 04 '22 09:10

Prasoon Saurav