Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 initializer with ambiguous function id-expression?

Tags:

c++

c++11

In the following C++11 code:

void f(int) {}
void f(double) {}

void (*p)(int) = f;

There are two functions.

The third f identifier is an id-expression and the initializer of p.

In 5.1.1p8 [expr.prim.general]/8 it says:

The type of the [id-expression] is the type of the identifier. The result is the entity denoted by the identifier. The result is an lvalue if the entity is a function, variable, or data member and a prvalue otherwise.

Given that f could be referring to two different entities with two different types, there is no "the entity" or "the type".

Is there some other text in the standard that addresses this situation?

Do implementations just disambiguate this as an extension or is it required somewhere? (Without some other text one could argue that an implementation could reject the f id-expression as ambiguous.)

like image 251
Andrew Tomazos Avatar asked Dec 18 '13 07:12

Andrew Tomazos


1 Answers

The standard (at § 13.4) defines that:

A use of an overloaded function name without arguments is resolved in certain contexts to a function, a pointer to function or a pointer to member function for a specific function from the overload set. A function template name is considered to name a set of overloaded functions in such contexts. The function selected is the one whose type is identical to the function type of the target type required in the context.

Emphasis mine.

After the quote, there is an example (at § 13.4/5) that resembles yours:

int f(double);
int f(int);
int (*pfd)(double) = &f; // selects f(double)
int (*pfi)(int) = &f; // selects f(int)

As far as the unary & is concerned, the standard specifies that (at § 5.3.1/6 and thanks to jogojapan):

The address of an overloaded function can be taken only in a context that uniquely determines which version of the overloaded function is referred to.

but can also be omitted (at § 13.4/1):

The overloaded function name can be preceded by the & operator.

(again, emphasis mine) just like you did, in your example.

like image 131
Shoe Avatar answered Nov 18 '22 18:11

Shoe