This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: static inline operator generates an external symbol
I'm sorry it has taken so long to get back to you on this, but I've been
out of town. Here is an example program that exhibits this behavious and
you will see that it is not in my code at all. I never declare the new
or delete operator yet it still makes an external reference. I've tried
compiling with and without the -fno-builtin flag. It has no affect on
this although the symbol is __builtin_xxx. Therefore, the compiler
itself must create the declaration regardless of using builtins or not.
Sean
"Martin v. Loewis" wrote:
> > I'm in a situation where I need to overload the standard new/delete
> > operators for a portion of my code. I defined them as static inline
> > inside an include file and everything compiles. When I go to link,
> > however, I get multiple definitions of __builtin_new, etc. Those
> > static inline operators are being exported. Is this a bug?
>
> Thanks for your bug report. This is a bug, both in your code, and in
> gcc. The bug in your code is that operator new is declared extern, and
> later declared static. The bug in g++ is that it does not detect this
> incorrect redeclaration.
>
> > Is there a better way to do this? There are too many object types
> > to overload for each of them.
>
> You could declare the operator new in a base class only. Other than
> that, there is no alternative. C++ does not support file-local
> allocation mechanisms.
>
> Regards,
> Martin
typedef __typeof(sizeof(0)) size_t;
#ifdef __cplusplus
extern "C" {
#endif
void *G2_Malloc (size_t);
void G2_Free (void *);
#ifdef __cplusplus
}
#endif __cplusplus
#define malloc(a) G2_Malloc(a)
#define free(a) G2_Free(a)
#ifdef __cplusplus
static void *operator new (size_t size) throw()
{
return malloc (size);
}
#ifndef ARM_B2_COMPILER_BUG // ARM_BUG: can't overload new[]
static void *operator new [] (size_t size) throw()
{
return malloc (size);
}
#endif // ARM_B2_COMPILER_BUG
static void operator delete (void *ptr)
{
free (ptr);
}
static void operator delete [] (void *ptr)
{
free (ptr);
}
#endif