Overload built-in (built-in?) Functions

Consider the following code:

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

double log(double) { return 42; }

int main() {
    std::cout << log(1) << std::endl;
}

During the build of the debug version, all used compilers (msvc, gcc, clang) print 42.

But when I try to build (and run) in release mode, I got:

  • compilation error in msvc: error C2169: 'log' : intrinsic function, cannot be defined;
  • prints 42for gcc;
  • prints 0for clang.

Why are the debug / debug results different for the same compiler?

Why were there different results for different compilers in release mode?

+4
source share
2 answers

You define a function that is already declared in <math.h>with an external connection.

C11, Β§7.12.6.7:

#include <math.h>
double log(double x);

Β§7.1.2:

.

[extern.names]/3:

Standard C, "C" link std, .

[reserved.names]/2 undefined; , , , .

+5

, (17.6.1.2.4):

++ ( , C) (3.3.6) std. , std - (7.3.3).

, log() math.h ( cmath) std . ( libstd++ gcc), log(1) , std::log(). clang, -, .

template <typename T> double log(T x);

, int, clang . ( , coliru clang, ).

+2

All Articles