Pimpl idiom without using dynamic memory allocation

Warning: the code here only showcases the storage aspect, it is a skeleton, no dynamic aspect (construction, copy, move, destruction) has been taken into account. I would suggest an approach using the C++0x new class aligned_storage, which is precisely meant for having raw storage. // header class Foo { public: private: struct Impl; Impl& impl() … Read more

Efficient linked list in C++?

Your requirements are exactly those of std::list, except that you’ve decided you don’t like the overhead of node-based allocation. The sane approach is to start at the top and only do as much as you really need: Just use std::list. Benchmark it: is the default allocator really too slow for your purposes? No: you’re done. … Read more

Why is the (virtual) destructor of the derived class not called when deleting an array through a pointer to the base class?

Note that whilst a Cat is an Animal, an array of Cats is not an array of Animals. In other words, arrays are invariant in C++, not covariant like they are in some other languages. So you are up-casting this array and this later confuses the compiler. You must do array delete[] in this case … Read more

Why use _mm_malloc? (as opposed to _aligned_malloc, alligned_alloc, or posix_memalign)

Intel compilers support POSIX (Linux) and non-POSIX (Windows) operating systems, hence cannot rely upon either the POSIX or the Windows function. Thus, a compiler-specific but OS-agnostic solution was chosen. C11 is a great solution but Microsoft doesn’t even support C99 yet, so who knows if they will ever support C11. Update: Unlike the C11/POSIX/Windows allocation … Read more

C++ polymorphism without pointers

Ultimately, no. Polymorphism only works with non-value types: references and pointers. And since references can only be bound once, you cannot really use them in standard containers. That leaves you with pointers. You’re attacking the problem at the wrong end. If you are concerned about the overhead of allocating lots of small objects (and I’m … Read more