可以C ++ 0x仍然显式地分配与全局运算符new? [英] Can C++0x still explicitly allocate with global operator new?

查看:133
本文介绍了可以C ++ 0x仍然显式地分配与全局运算符new?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

维基百科说明:


一个类型可能无法使用operator new分配:

  struct NonNewable {
void * operator new(std :: size_t)= delete;
};

此类型的对象只能分配为堆栈对象或另一个类型的成员。它不能直接堆分配没有非便携的欺骗。 (因为placement new是在用户分配的内存上调用构造函数的唯一方法,并且这种使用已经被禁止,因此对象不能被正确构造。)


删除运算符new类似于在当前C ++中使用私有,但是没有显式地使用全局运算符new,这避免了类特定的查找,仍然有效C ++ 0x?

  NonNewable * p = :: new NonNewable(); 
//既不是非便携也不是欺骗,虽然可能不是广为人知






要清楚,这是有效的C ++ 03和工作正常

  struct NonNewable {
private:
void * operator new(std :: size_t); // not defined
};

int main(){
//忽略泄漏,只是一个例子

void * mem = operator new(sizeof(NonNewable));
NonNewable * p = :: new(mem)NonNewable();

p = :: new NonNewable();

return 0;
}


解决方案

我相信你是对的,维基百科是错误的。 C ++ 0x标准草案将删除的函数(8.4p10)描述为不能以任何方式使用的函数(否则程序不成形)。它们在范围或名称查找中不起作用,不同于正常功能。关于新表达式的相关段落保持不变:


[5.3.4p8] new-expression通过调用分配函数(3.7.4.1)。 ...

[5.3.4p9]如果new-expression以unary ::运算符开头,则在全局范围中查找分配函数的名称。否则,如果分配的类型是类类型T或其数组,则在T的范围内查找分配函数的名称。如果该查找无法找到名称,或者如果分配的类型不是类类型,则分配


所以是的,表达式 :: new NonNewable [或 :: new(mem)NonNewable ]将选择 :: operator new ,忽略函数 NonNewable :: operator new ,并且不会使程序生成错误。


Wikipedia states:

A type can be made impossible to allocate with operator new:

struct NonNewable {
    void *operator new(std::size_t) = delete;
};

An object of this type can only ever be allocated as a stack object or as a member of another type. It cannot be directly heap-allocated without non-portable trickery. (Since placement new is the only way to call a constructor on user-allocated memory and this use has been forbidden as above, the object cannot be properly constructed.)

Deleting operator new is similar to making it private in current C++, but isn't explicitly using global operator new, which avoids class-specific lookup, still valid C++0x?

NonNewable *p = ::new NonNewable();
// neither non-portable nor trickery, though perhaps not widely known

Have I missed something in the draft?


To be clear, this is valid C++03 and works fine:

struct NonNewable {
private:
  void *operator new(std::size_t);  // not defined
};

int main() {
  // ignore the leaks, it's just an example

  void *mem = operator new(sizeof(NonNewable));
  NonNewable *p = ::new(mem) NonNewable();

  p = ::new NonNewable();

  return 0;
}

解决方案

I believe you are right and wikipedia is wrong. The C++0x draft standard describes "deleted functions" (8.4p10) as functions which may not be used in any way (or else the program is ill-formed). They play no part in scope or name lookup different from normal functions. And the relevant paragraphs concerning new expressions have remained the same:

[5.3.4p8] A new-expression obtains storage for the object by calling an allocation function (3.7.4.1). ...

[5.3.4p9] If the new-expression begins with a unary :: operator, the allocation function's name is looked up in the global scope. Otherwise, if the allocated type is a class type T or array thereof, the allocation function's name is looked up in the scope of T. If this lookup fails to find the name, or if the allocated type is not a class type, the allocation function's name is looked up in the global scope.

So yes, the expression ::new NonNewable [or ::new(mem) NonNewable] would choose an overload of ::operator new, ignoring the function NonNewable::operator new, and would not make the program ill-formed.

这篇关于可以C ++ 0x仍然显式地分配与全局运算符new?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆