Bug 10943 - ext/hash_map causes namespace issues with abs
Summary: ext/hash_map causes namespace issues with abs
Status: RESOLVED DUPLICATE of bug 11409
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 3.3
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 11472 (view as bug list)
Depends on:
Blocks:
 
Reported: 2003-05-22 18:44 UTC by kenneth.simpson
Modified: 2003-09-07 04:54 UTC (History)
3 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2003-05-31 08:32:36


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description kenneth.simpson 2003-05-22 18:44:15 UTC
The following code on a RH7.2 machine will compile with gcc 3.2.2, but not 3.3.
  #include <stdlib.h>
  inline static double abs(double a) { return a>=0? a : -a; }
  #include <ext/hash_map>

3.3 complains:
/include/c++/3.3/cmath: In function `double std::abs(double)':
/include/c++/3.3/cmath:172: error: `double std::abs(double)' conflicts with
previous using declaration `double abs(double)'


Weeding through the preprocessed output (g++ -E foo.cc | egrep -e "\babs\b"), I
see that 322 has the following declarations:
  extern int abs (int __x) throw () __attribute__ ((__const__));
  inline static double abs(double a) { return a>=0? a : -a; }
    using ::abs;
    abs(long __i) { return labs(__i); }
    abs(long long __x) { return __x >= 0 ? __x : -__x; }
    using __gnu_cxx::abs;

The first is from <stdlib.h>, the second is from foo.cc, and the last 4 result
from <ext/hash_map> pulling in <cstdlib>

Using this to whittle down the test case I get:

  extern "C" {
    extern int abs (int __x) throw () __attribute__ ((__const__));
    extern long int labs (long int __x) throw () __attribute__ ((__const__));
  }

  inline static double abs(double a) { return a>=0? a : -a; }

  namespace std {
    using ::abs;
    long abs(long __i) { return labs(__i); }
  }

  namespace __gnu_cxx {
    long long abs(long long __x) { return __x >= 0 ? __x : -__x; }
  }

  namespace std {
    using __gnu_cxx::abs;
  }

Which compiles on both 3.2.2 and 3.3... however...


Looking at the preprocessed 3.3 out put I see:

extern int abs (int __x) throw () __attribute__ ((__const__));
inline static double abs(double a) { return a>=0? a : -a; }
  using ::abs;
  abs(long __i) { return labs(__i); }
  abs(long long __x) { return __x >= 0 ? __x : -__x; }
  using __gnu_cxx::abs;
  abs(double __x)
  abs(float __x)
  abs(long double __x)

The three final lines are new and come from <cmath>.

Using this to produce a test case yields:
  extern "C" {
    extern int abs (int __x) throw () __attribute__ ((__const__));
    extern long int labs (long int __x) throw () __attribute__ ((__const__));
  }

  inline static double abs(double a) { return a>=0? a : -a; }

  namespace std {
    using ::abs;
    long abs(long __i) { return labs(__i); }
  }

  namespace __gnu_cxx {
    long long abs(long long __x) { return __x >= 0 ? __x : -__x; }
  }

  namespace std {
    using __gnu_cxx::abs;
  }

  // new part:
  namespace std {
    inline double abs(double __x) { return __builtin_fabs(__x); }
    inline float abs(float __x) { return __builtin_fabs(__x); }
    inline long double abs(long double __x) { return __builtin_fabs(__x); }
  }

Which neither 3.2.2, nor 3.3 like, both complaining:
  In function `double foo.cc:std::abs(double)':
  foo.cc:32: error: `double std::abs(double)' conflicts with previous using
declaration `double abs(double)'

Another data point:
  Just replacing <ext/hash_map> with <cmath> does not cause this.

Hope this was useful.
-Kenny
This was with "gcc version 3.3" and "gcc version 3.2.2"
Comment 1 Dara Hazeghi 2003-05-31 08:32:36 UTC
Hello,

I can confirm that this problem is present on gcc 3.3 branch and mainline (20030525).

Dara
Comment 2 Andrew Pinski 2003-07-09 12:30:06 UTC
*** Bug 11472 has been marked as a duplicate of this bug. ***
Comment 3 Andrew Pinski 2003-07-09 12:35:21 UTC
This is a dup of 11409 which is caused by builtins.

*** This bug has been marked as a duplicate of 11409 ***
Comment 4 Andrew Pinski 2003-09-07 04:54:54 UTC
Actually this was not a dup (the one I marked as a dup of this one was really a dup of the 
other bug) but invalid as the extern "C" stuff is right in C++ and the abs function in the 
namespace std has to be the same function (point to the same memory) and including 
one of the standard headers can include others.