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]

Re: operator new and delete overloading problems.


> When overloading operator new in global space and also in a
> namespace the assembler gets allready defined symbol of
> building_new. I excluded a lot of info to make the example small.

Thanks for your bug report. Here is a patch that fixes the bug.

I assume your other problem is solved with this patch as well. If not,
please re-report.

I cannot understand why you have to call the function inside namespace
Foo "operator new", though - any other name (say, bar) would work just
as well, wouldn't it?

Regards,
Martin

2000-01-14  Martin v. Löwis  <loewis@informatik.hu-berlin.de>

	* method.c (build_decl_overload_real): Check whether we are in ::
	before returning __builtin_new/delete.

// Test whether N::operator new is different from ::operator new
#include <new>
#include <cstdlib>

bool success;

namespace N{
  void* operator new(size_t n){
    success = true;
    return std::malloc(n);
  }
}

void *operator new(size_t n)throw(std::bad_alloc)
{
  static bool entered = false;
  if(entered)
    throw std::bad_alloc();
  entered = true;
  void *result = N::operator new(n);
  entered = false;
  return result;
}

int main()
{
  try{
    new int;
  }catch(...){
    return 1;
  }
  return success?0:1;
}

Index: method.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/method.c,v
retrieving revision 1.130
diff -u -r1.130 method.c
--- method.c	1999/12/29 07:31:51	1.130
+++ method.c	2000/01/14 22:24:01
@@ -1,6 +1,6 @@
 /* Handle the hair of processing (but not expanding) inline functions.
    Also manage function and variable name overloading.
-   Copyright (C) 1987, 89, 92-97, 1998, 1999 Free Software Foundation, Inc.
+   Copyright (C) 1987, 89, 92-99, 2000 Free Software Foundation, Inc.
    Contributed by Michael Tiemann (tiemann@cygnus.com)
 
 This file is part of GNU CC.
@@ -1579,7 +1579,8 @@
   const char *name = IDENTIFIER_POINTER (dname);
 
   /* member operators new and delete look like methods at this point.  */
-  if (! for_method && parms != NULL_TREE && TREE_CODE (parms) == TREE_LIST
+  if (! for_method && current_namespace == global_namespace
+      && parms != NULL_TREE && TREE_CODE (parms) == TREE_LIST
       && TREE_CHAIN (parms) == void_list_node)
     {
       if (dname == ansi_opname[(int) DELETE_EXPR])

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