namespace X
{
void* operator new (size_t);
}
gives error message as:
error: ‘void* X::operator new(size_t)’ may not be declared within a namespace
Is it a gcc compiler bug ? In older gcc version it seems to be working. Any idea, why it's not allowed ?
Use case:
I wanted to allow only custom operator new/delete
for the classes and wanted to disallow global new/operator
. Instead of linker error, it was easy to catch compiler error; so I coded:
namespace X {
void* operator new (size_t);
}
using namespace X;
This worked for older version of gcc but not for the new one.
@Sharptooth's Answer makes more sense if we consider this section from the standard:
3.7.3.1 Allocation functions [basic.stc.dynamic.allocation]
The above limitation is probably imposed for the very reason that @sharptooth's answer points out.
This is not allowed because it makes no sense. For example you have the following
now how should the
ptr
bedelete
d - with global namespaceoperator delete()
or with the one specific tonamespace X
? There's no possible way for C++ to deduce that.