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
c ++ g ++ clang ++


source to share


No one has answered this question yet

See similar questions:

64
Why is my log in the std namespace?

or similar:

3076
What are the differences between a pointer variable and a reference variable in C ++?
2101
What is the difference between #include <filename> and #include "filename"?
1994
What are the basic rules and idioms for operator overloading?
783
What is the difference between g ++ and gcc?
ten
Declaring a member function using function type syntax
3
Operator "<<" overloads the return type
3
Operator << overloading hides others
2
C ++: confusion over function overloading and declaration order
1
g ++ and clang ++ different behavior when overloading operator <()
0
Simple Protocol Buffers works when compiled with g ++ but not with the g ++ clan



All Articles
Loading...
X
Show
Funny
Dev
Pics