Hello Gurus,

I'm working on a few generic code sequences which should in some ways mimic the functionality of global operator new. I believe the code might be considered in bad taste. However, I would still be curious if there are syntaxes available for this.

I want to mimic new in so far as it takes (or allocates) a chunk of memory and calls a ctor on it.

My codes are below. The upper ctor call works for VS2010 but not for GCC 4.5.0. The template works for neither.

  • Can anyone help out with the syntaxes of these codes?
  • Is it even possible to call ctors directly on a chunk of memory in these ways?

Thanks. Sincerely, Chris.

#include <iostream>

typedef unsigned char UINT8;

struct Wrapper
  unsigned x;
  Wrapper(const unsigned X = 1u) : x(X) { }

  UINT8 pool[64u];

template<typename T> T* alloc(void)
  ((T*) pool)->T::T();

int main()
  // Can not call constructor directly?
  // Is there a syntax for this?
  ((Wrapper*) pool)->Wrapper::Wrapper();
  const Wrapper* p1 = (Wrapper*) pool;
  std::cout << p1->x << std::endl;

  // Does not seem to work.
  // Wrapper does not have member function T.
  // Any syntax for this?
  const Wrapper* p2 = alloc<Wrapper>();
  std::cout << p2->x << std::endl;