From the C++11 draft, 7.5 (para. 1):
Two function types with different language linkages are distinct types even if they are otherwise identical.
So I can do overload based on language linkages:
extern "C" typedef void (*c_function)();
typedef void (*cpp_function)();
void call_fun(c_function f)
{
}
void call_fun(cpp_function f)
{
}
extern "C" void my_c()
{
}
void my_cpp()
{
}
int main()
{
call_fun(my_c);
call_fun(my_cpp);
}
But, with GCC 4.7.1 this sample code gives the error messages:
test.cpp: In function 'void call_fun(cpp_function)':
test.cpp:7:6: error: redefinition of 'void call_fun(cpp_function)'
test.cpp:4:6: error: 'void call_fun(c_function)' previously defined here
And with CLang++ :
test.cpp:7:6: error: redefinition of 'call_fun'
void call_fun(cpp_function f)
^
test.cpp:4:6: note: previous definition is here
void call_fun(c_function f)
^
Now the questions:
Is my understanding of the standard correct? Is this code valid?
Does anybody know if these are bugs in the compilers or if they are intentionally doing it that way for compatibility purposes?