https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70834

            Bug ID: 70834
           Summary: Incorrect warning for placement new when conditionally
                    used
           Product: gcc
           Version: 6.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: matt at godbolt dot org
  Target Milestone: ---

Consider some code which tries to placement-new into an internal buffer (if the
object fits), else uses the heap:

--

#include <new>
struct Test {
  char buf[4];

  template<typename T> T *construct() {
    if (sizeof(T) <= sizeof(buf))
        return new ((void*)&buf)T;  // if it fits, use the buf
    else return new T;  // else make it on the heap
  }
};

void t1(Test &t) {
  t.construct<int>();
}

void t2(Test &t) {
  t.construct<double>();
}

-- (also at https://godbolt.org/g/p5Lpnz)

GCC 6.1 correctly generates code in t2 to use the "new T", and placement new
code for t1.

However, there's a spurious warning: placement new constructing an object of
type 'double' and size '8' in a region of type 'char [4]' and size '4'
[-Wplacement-new=] - which is not the case in this code. In code I'm now
compiling I've been able to #pragma this warning off. I'll try and resort to
template trickery to dispatch on the sizeof(), but early attempts also caused
the warning too, so it may be the case this warning's tough to work around.

Reply via email to