This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/13220] New: Cannot overload local operator new and include <iostream>


A conflict exists between a module local overload of operator new and the 
definition of operator new that gets dragged in by <iostream>.  The following 
code fails to compile.  

The problem is caused by headers pulled in by <iostream> which can be verified 
by commenting out the include of <iostream>.

---- sample2.cpp ----
#include <stdlib.h>
#include <iostream>
//
static void* operator new(size_t size) { malloc(size); }
static void operator delete(void* p) { free(p); }
static void* operator new[](size_t size) { malloc(size); }
static void operator delete[](void* p) { free(p); }

int  main(){ return 0; }
---- EOF ----

How-To-Repeat:
$ g++ sample2.cpp -o sample
sample2.cpp: In function `void* operator new(unsigned int)':
sample2.cpp:4: error: `void* operator new(unsigned int)' was declared `extern'
   and later `static'
/usr/local/gcc-3.3.2/include/c++/3.3.2/new:82: error: previous declaration of `
   void* operator new(unsigned int)'
sample2.cpp: In function `void operator delete(void*)':
sample2.cpp:5: error: `void operator delete(void*)' was declared `extern' and
   later `static'
/usr/local/gcc-3.3.2/include/c++/3.3.2/new:84: error: previous declaration of `
   void operator delete(void*)'
...,etc.

Workaround:
The problem can be worked around by moving the include of <iostream> until 
after the code that overloads operator new and new[].  This won't work if the 
overloaded methods need to use <iostream>, however.

-- 
           Summary: Cannot overload local operator new and include
                    <iostream>
           Product: gcc
           Version: 3.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tom at storagematrix dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13220


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]