What are some uses of decltype(auto)?

In c++14 the decltype(auto) idiom is introduced. Typically its use is to allow auto declarations to use the decltype rules on the given expression. Searching for examples of “good” usage of the idiom I can only think of things like the following (by Scott Meyers), namely for a function’s return type deduction: template<typename ContainerType, typename … Read more

Lambda capture as const reference?

Is it possible to capture by const reference in a lambda expression? I want the assignment marked below to fail, for example: #include <algorithm> #include <string> using namespace std; int main() { string strings[] = { “hello”, “world” }; static const size_t num_strings = sizeof(strings)/sizeof(strings[0]); string best_string = “foo”; for_each( &strings[0], &strings[num_strings], [&best_string](const string& s) … Read more

Can modern C++ get you performance for free?

It is sometimes claimed that C++11/14 can get you a performance boost even when merely compiling C++98 code. The justification is usually along the lines of move semantics, as in some cases the rvalue constructors are automatically generated or now part of the STL. Now I’m wondering whether these cases were previously actually already handled … Read more

How to implement classic sorting algorithms in modern C++?

The std::sort algorithm (and its cousins std::partial_sort and std::nth_element) from the C++ Standard Library is in most implementations a complicated and hybrid amalgamation of more elementary sorting algorithms, such as selection sort, insertion sort, quick sort, merge sort, or heap sort. There are many questions here and on sister sites such as https://codereview.stackexchange.com/ related to … Read more