Why doesn’t this “undefined extern variable” result in a linker error in C++17?

Because the variable isn’t odr-used. You have a constexpr if there that always discards the branch that could use it. One of the points of constexpr if is that the discarded branch need not even compile, only be well-formed. That’s how we can place calls to non-existing member functions in a discarded branch.

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

What are aggregates and trivial types/PODs, and how/why are they special?

How to read: This article is rather long. If you want to know about both aggregates and PODs (Plain Old Data) take time and read it. If you are interested just in aggregates, read only the first part. If you are interested only in PODs then you must first read the definition, implications, and examples … 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