Clang ++ vs g ++ difference in ordering an overloaded function declaration

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

.

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 cos

and prints 10

, but with g++

it calls the version in the math library and prints 0.540302

.

I wonder what 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 in this case.

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

not being used.

+3


source to share





All Articles