3

I found a difference in clang++ vs g++ behavior when adding an overloaded function definition for something in the <cmath> library.

Specifically, in this program:

#include <cmath>
#include <iostream>

double cos(double x) throw();

int main() {
  std::cout << cos(1.0) << std::endl;
  return 0;
}

double cos(double x) throw() {
  return 10;
}

when I compile with clang++, it calls my overloaded version of cos and prints 10, but with g++ it calls the version in the math library and prints 0.540302.

Interestingly, g++ will also call my overloaded cos if I put the function definition (not just the prototype) before main.

Is there some unspecified behavior here, or a bug in one of these compilers? I can't figure out what the standard says should happen in this case.

I have tried this with multiple versions of both compilers and get the same behavior, with no warnings except that parameter x is unused.

Dan R
  • 1,412
  • 11
  • 21

0 Answers0