Do I really have to worry about alignment when using placement new operator?

When you call placement new on a buffer: A *a = new (buf) A; you are invoking the built-in void* operator new (std::size_t size, void* ptr) noexcept as defined in: c++11 18.6.1.3 Placement forms [new.delete.placement] These functions are reserved, a C++ program may not define functions that displace the versions in the Standard C++ library … Read more

How to initialize all elements in an array to the same number in C++ [duplicate]

I’m surprised at all the answers suggesting vector. They aren’t even the same thing! Use std::fill, from <algorithm>: int directory[100]; std::fill(directory, directory + 100, -1); Not concerned with the question directly, but you might want a nice helper function when it comes to arrays: template <typename T, size_t N> T* end(T (&pX)[N]) { return pX … Read more

std::enable_if : parameter vs template parameter

Default template arguments are not part of the signature of a template (so both definitions try to define the same template twice). Their parameter types are part of the signature, however. So you can do template <class T> class check { public: template< class U = T, typename std::enable_if<std::is_same<U, int>::value, int>::type = 0> inline static … Read more

std::this_thread::sleep_for() and GCC

Confirmed that it doesn’t work here as well. (Recent GCC 4.6 snapshot). You could do the obvious and simply define it before you include any std:: headers. A bit dirty but will work until GCC fixes it (unless this is intended behavior). The #define shouldn’t break anything anyways. Either in source or -D_GLIBCXX_USE_NANOSLEEP flag to … Read more

Makefile removes object files for no reason

The files are being removed because make considers them “intermediate”. When make forms a chain of rules to produce a prerequisite, it treats all files created by the intermediate chains as “intermediate” and removes then when the target is created. See Chained Rules in the manual for GNU make. In your case, you can prevent … Read more

What is a .h.gch file?

A .gch file is a precompiled header. If a .gch is not found then the normal header files will be used. However, if your project is set to generate pre-compiled headers it will make them if they don’t exist and use them in the next build. Sometimes the *.h.gch will get corrupted or contain outdated … Read more