c++ - overloaded global new operator issue with msvc2008 -
i'm working @ own library, , have own allocators. i've declared common interface this:
class myallocator { public: void * allocate(size_t size); void * allocate(size_t size, size_t align); void deallocate(void *); //... };
now, if want allocate c++ objects allocators must use placement new in manner:
myallocator a; myobject * o = new(a.allocate(sizeof(myobject))) myobject(param1, param2, ...);
this, of course, works pretty well. now, time ago wanted make global allocator, take allocator parameter, in order avoid repetitive sizeof(). come this:
template< typename allocatort > inline void *operator new(size_t n_bytes, allocatort & allocator) { return allocator.allocate(n_bytes); }
with this, can call:
myallocator my_alloc; myobject * o = new(my_alloc) myobject(param1, param2);
a syntax clean, , cool. works pretty (with gcc, , msvc2010), today, when have tried in msvc2008 got errors: that's because of msvc2008 compiler fooled template parameter, so, when include header use normal placement new [almost every stl header contain call placement new, e.g. vector, set, etc] compiler use templated version of global new, instead of placement new, causing obvious error: type 'void*' not class/struct , of course not have allocate() member function.
now, questions arise:
- is bug of msvc2008? gcc 4.4.0, 4.4.5 , msvc2010 works pretty well.
- am wrong in writing templated global new operator accepts allocator reference? mean, can thing ambiguous syntax can compilers fooled easily, causing errors, , should abandon idea, or can doable in other way? noticed before, reduces complexity of c++ objects allocation custom allocator using normal placement new.
normally, if have:
void f(void *); //1 template< typename >f(a &); //2
and call:
void * void_ptr = something(); f(void_ptr);
of course here first version called.
why seems not happen in msvc2008 templated version of operator new?
Comments
Post a Comment