Mixing operator new[] and placement new with ordinary delete[]

前端 未结 5 2087
悲&欢浪女
悲&欢浪女 2020-12-18 20:10

Just out of curiosity, is the following legal?

X* p = static_cast(operator new[](3 * sizeof(X)));
new(p + 0) X();
new(p + 1) X();
new(p + 2) X();

         


        
5条回答
  •  旧时难觅i
    2020-12-18 20:23

    I think that cannot be legal. Because that implies these equations:

    new-expression    = allocation-function  +  constructor
    delete-expression = destructor  +  deallocation-function
    

    Nothing more, nothing less. But the Standard does not say exactly that, as far as I know. It might be possible that new-expression does more than allocation-function + constructor together do. That is, the actual equations could be this, and the Standard doesn't forbid it explicitly anywhere:

    new-expression    = allocation-function  +  constructor   +  some-other-work
    delete-expression = destructor  +  deallocation-function  +  some-other-work
    

提交回复
热议问题