C++ Standards – Handling const Pointers in Overload Resolution

c++standards

GCC treats these two function declarations as equivalent:

void F(int* a) { }
void F(int* const a) { }

test.cpp: In function 'void F(int*)':

test.cpp:235: error: redefinition of 'void F(int*)'

test.cpp:234: error: 'void F(int*)' previously defined here

This makes some sense because a caller will always ignore the const in this case… it only affects the usage of the parameter 'a' inside of the function.

What I'm wondering is where (if anywhere) the standard says that it's specifically OK to discard qualifiers on pointers used as function arguments for the purpose of overload resolution.

(My real issue is that I'd like to figure out where GCC strips these pointless qualifiers internally, and since the C++ frontend of GCC is littered with comments referencing the standard, the relevant section of the standard might help me find the correct spot.)

Best Answer

Standard says in 8.3.5/3 that for the purposes of determining the function type any cv-qualifiers that directly qualify the parameter type are deleted. I.e. it literally says that a function declared as

void foo(int *const a);

has function type void (int *).

A pedantic person might argue that this is not conclusive enough to claim that the above declaration should match the definition like this one

void foo(int *a)
{
}

or that it should make the code with dual declaration (as in your example) ill-formed, since neither of these concepts are described in the standard in terms of function types.

I mean, we all know that these const were intended to be ignored for all external purposes, but so far I was unable to find the wording in the standard that would conclusively state exactly that. Maybe I missed something.

Actually, in 13.1/3 it has a "Note" that says that function declarations with equivalent parameter declarations (as defined in 8.3.5) declare the same function. But it is just a note, it is non-normative, which suggests that somewhere in the standard there should be some normative text on the same issue.

Related Question