Why does not a compiler time error occur when calling the C function

In accordance with the example that the Standard provides with N4296::13.3.3 [over.match.best]

 namespace A { extern "C" void f(int = 5); } namespace B { extern "C" void f(int = 5); } using A::f; using B::f; void use() { f(3); // OK, default argument was not used for viability f(); // Error: found default argument twice } 

As stated in the Standard in N4296::7.5/6 [dcl.link] :

Two declarations for a function with a C-language connection with the same function name (ignoring the namespace names that qualify it) that appear in different areas of the namespace refer to the same function.

I tried to learn such a thing with my own example:

 #include <iostream> namespace A { extern "C" void foo(int a = 5){ std::cout << a << "1" << std::endl; } } namespace B { extern "C" void foo(int a = 5); } using A::foo; using B::foo; int main() { foo(); //Error foo(2); } 

Demo

So why does my example work? What are the differences between my example and the standard example if I have not defined the function explicitly in the namespace A ? Why is this so important?

+7
c ++ c
source share
1 answer

As noted in the comments, there is no significant difference between the standard example and your example. Compilers that properly implement the standard release are diagnostic for both.

The fact that this is clearly a compiler error, at least clang, and Intel can be seen when you edit this example for meaningless

 namespace A { extern "C" void f(int = 5); } namespace B { extern "C" void f(int = 3); // different default argument } using A::f; using B::f; void use() { f(); // No error ! } 

Despite receiving two different arguments by default, no error or even warning occurs. One of the default arguments is used, the first with Intel, the second with clang.

GCC really rejects this senseless example, so there is no simple and easy way to verify that it is clearly a mistake in GCC, but this does not change the fact that it is: as noted, it silently accepts an example from the standard, where the standard indicates where an error should be detected.

+1
source share

All Articles