Return value of placement new

后端 未结 2 427
挽巷
挽巷 2021-02-05 03:11

Consider the following C++14 code:

#include 
#include 
#include 

struct NonStandardLayout
{
    // ...
};

int main         


        
2条回答
  •  不思量自难忘°
    2021-02-05 03:49

    I'm surprised no one has mentioned this cheap counterexample yet:

    struct foo {
      static foo f;
      // might seem dubious but doesn't violate anything
      void* operator new(size_t, void* p) {return &f;}
    };
    

    Demo on Coliru.

    Unless a class-specific placement version is called, however, your assertion should hold. Both expressions have to represent the same address as explained in the other answer (the main point being that the standard non-allocating operator new simply returns the pointer argument and the new expression not doing anything fancy), and neither of these is a pointer past the end of some object so, by [expr.eq]/2, they compare equal.

提交回复
热议问题