How to enable C++17 on Mac?

On my 10.11 El Capitan, Xcode 7.3.1, clang has been updated to: Apple LLVM version 7.3.0 (clang-703.0.31) which is almost equivalent to llvm version 3.8. clang++ hasn’t -std=c++17 option, but -std=c++1z, working well at present, though only supporting some features of C++1z. For gcc, you can install a very new one by: brew install gcc …

Read more

How to check if a pointer points to a properly aligned memory location?

If the remainder isn’t zero when dividing the address with the desired alignment, then the address isn’t aligned. inline bool is_aligned(const void * ptr, std::uintptr_t alignment) noexcept { auto iptr = reinterpret_cast<std::uintptr_t>(ptr); return !(iptr % alignment); } Ths can’t be constexpr though, because of the cast. Also, this relies on the implementation-defined fact that the …

Read more

Try to understand compiler error message: default member initializer required before the end of its enclosing class

This is a clang and gcc bug, we have a clang bug report for this: default member initializer for ‘m’ needed within definition of enclosing class for default argument of function which has the following example: #include <limits> class A { public: class B { public: explicit B() = default; ~B() = default; private: double …

Read more

Bit count : preprocessor magic vs modern C++

Why not use the standard library? #include <bitset> int bits_in(std::uint64_t u) { auto bs = std::bitset<64>(u); return bs.count(); } resulting assembler (Compiled with -O2 -march=native): bits_in(unsigned long): xor eax, eax popcnt rax, rdi ret It is worth mentioning at this point that not all x86 processors have this instruction so (at least with gcc) you …

Read more