Must template argument functions be treated as potentially constexpr?

Introduction template<int F(), int N = F()> void func (); In this answer we will go through the relevant sections of the International Standard, step by step, to prove that the above snippet is well-formed. What does the International Standard (N3337) say? The Standardese 14.1p9 Template parameters [temp.param] A default template-argument is a template-argument (14.3) … Read more

Is using `std::get` on a `std::tuple` guaranteed to be thread-safe for different values of `I`?

Since std::get has no explicit statements in the specification about its data race properties, we fall back to the default behavior defined in [res.on.data.races]. Specifically, paragraphs 2 and 3 tell the story: A C++ standard library function shall not directly or indirectly access objects (1.10) accessible by threads other than the current thread unless the … Read more

Is it legal to declare a constexpr initializer_list object?

Update: The situation got a bit more complicated after the resolution of CWG DR 1684 removed the requirement quoted below. Some more information can be found in this discussion on the std-discussion mailing list and in the related question Why isn’t `std::initializer_list` defined as a literal type? [decl.constexpr]/8: A constexpr specifier for a non-static member … Read more

Avoiding self assignment in std::shuffle

The libstdc++ Debug Mode assertion is based on this rule in the standard, from [res.on.arguments] If a function argument binds to an rvalue reference parameter, the implementation may assume that this parameter is a unique reference to this argument. i.e. the implementation can assume that the object bound to the parameter of T::operator=(T&&) does not … Read more

Quick sort at compilation time using C++11 variadic templates

Have you also looked at its memory consumption? Note that quicksort itself is worse than linear, with a quite bad worse case runtime. This multiplies with the worse than linear runtime behaviour of certain steps of template compilation and instantiation (sometimes those are exponentional). You should maybe graph your compiletime for various datasets to observe … Read more

When is a `thread_local` global variable initialized?

The standard allows for this behavior, although it doesn’t guarantee it. From 3.7.2/2 [basic.stc.thread]: A variable with thread storage duration shall be initialized before its first odr-use (3.2) and, if constructed, shall be destroyed on thread exit. It’s also possible that the objects are constructed at some other time (e.g. on program startup), as “before … Read more

Transparent Operator Functors

The transparent operator functors proposal is there as a way to have generalised functors that are located in <functional>. I personally believe the proposal itself has a very good example that would help illustrate the need for it. However I’ll go ahead and try to explain it as well. Suppose you have a function, a … Read more

Benefits of using reference_wrapper instead of raw pointer in containers?

I don’t think there is any technical difference. Reference wrapper provides basic pointer functionality, including the ability to change the target dynamically. One benefit is that it demonstrates intent. It tells people who read the code that “whoever” has the variable, isn’t actually controlling its lifespan. The user hasn’t forgotten to delete or new anything, … Read more