operator new inside namespace

后端 未结 2 1553
栀梦
栀梦 2020-12-11 05:19
namespace X
{
  void* operator new (size_t);
}

gives error message as:

error: ‘void* X::operator new(size_t)’ may not be declared w         


        
2条回答
  •  不思量自难忘°
    2020-12-11 05:42

    @Sharptooth's Answer makes more sense if we consider this section from the standard:

    3.7.3.1 Allocation functions [basic.stc.dynamic.allocation]

    [..] An allocation function shall be a class member function or a global function; a program is ill-formed if an allocation function is declared in a namespace scope other than global scope or declared static in global scope. [..]

    The above limitation is probably imposed for the very reason that @sharptooth's answer points out.

提交回复
热议问题