Search code examples
c++stdlinkage

standard library not raising error for duplicate definition


Why does this not return an error for duplicate function definition since the c++ standard library does external linkage.

This shouldn't be function overloading since its is type double(double,double) which is the exact same as the pow defined in math.h

#include <iostream>
#include <math.h>

double pow(double base, double exponent) {
    return 1;
}

int main() {
    std::cout << pow(2, 2);
}

Solution

  • The function signature pow(double, double) is reserved to the language implementation in the global namespace. By defining a reserved name, the behaviour of the program is undefined.

    Then the behaviour of the program is undefined, you are not guaranteed to be "raised an error for duplicate definition".

    Related standard rules (from latest draft):

    [reserved.names.general]

    If a program declares or defines a name in a context where it is reserved, other than as explicitly allowed by [library], its behavior is undefined.

    [extern.names]

    Each name from the C standard library declared with external linkage is reserved to the implementation for use as a name with extern "C" linkage, both in namespace std and in the global namespace.

    Each function signature from the C standard library declared with external linkage is reserved to the implementation for use as a function signature with both extern "C" and extern "C++" linkage, or as a name of namespace scope in the global namespace.