[Bug c++/70834] Incorrect warning for placement new when conditionally used

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Jan 3 23:06:00 GMT 2017


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

--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
As a workaround until the warning is made smarter, partial specialization can
be used to avoid the compile-time conditional, for example like so:

struct Test {
  char buf[4];

  template<typename T, bool>
  struct Ctor {
    static T *construct(void*) {
      return new T;  // else make it on the heap
    }
  };

  template<typename T> T* construct () {
    return Ctor<T, sizeof (T) <= sizeof Test::buf>::construct (buf);
  }
};

template<typename T> 
struct Test::Ctor<T, true> {
  static T* construct (void *p) {
    return new (p) T;
  }
};


More information about the Gcc-bugs mailing list