Default template parameter & lambda in unevaluated context: bug or feature?

Could someone provide an explanation of the rules that make ok3 true but ok4 false? ok3 is true because uses lambdas type as default type. The type of a lambda-expression (which is also the type of the closure object) is a unique, unnamed non-union class type, Hence, default template type for object, template parameter type … Read more

Which headers in the C++ standard library are guaranteed to include another header?

This answer ignores C headers – both the <meow.h> and <cmeow> ones. Of the C++ library headers (all references are to N4659): <initializer_list> is guaranteed to be included by: <utility> (§23.2.1 [utility.syn]) <string> (§24.3.1 [string.syn]) <array> (§26.3.2 [array.syn]) <deque> (§26.3.3 [deque.syn]) <forward_list> (§26.3.4 [forward_list.syn]) <list> (§26.3.5 [list.syn]) <vector> (§26.3.6 [vector.syn]) <map> (§26.4.2 [associative.map.syn]) <set> (§26.4.3 … Read more

Differences between std::is_convertible and std::convertible_to (in practice)?

std::is_convertible<From, To> (the type trait) checks is type From is implicitly convertible to type To. std::convertible_to<From, To> (the concept) checks that From is both implicitly and explicitly convertible to To. It’s rare that this is not the case, such types are ridiculous, but it’s nice in generic code to just not have to worry about … Read more

C++ check if statement can be evaluated constexpr

Here’s another solution, which is more generic (applicable to any expression, without defining a separate template each time). This solution leverages that (1) lambda expressions can be constexpr as of C++17 (2) the type of a captureless lambda is default constructible as of C++20. The idea is, the overload that returns true is selected when … Read more

How to use C++ 20 in g++

C++20 features are available since GCC 8. To enable C++20 support, add the command-line parameter -std=c++20 For G++ 9 and earlier use -std=c++2a Or, to enable GNU extensions in addition to C++20 features, add -std=gnu++20

C++20 ranges too many | operators?

TL;DR: In this case, the iterator type of the result of std::views::take is std::counted_iterator, which sometimes fails to model an iterator concept when it’s not expected to fail. This is LWG 3408 and is resolved by P2259. This involves some really complicated mechanism. Let T be the iterator type of values | std::views::filter(even) | std::views::transform(square), … Read more

Why is C++20’s `std::popcount` restricted to unsigned types?

Pretty simple: Implicit widening conversions on unsigned types do not change the result. Implicit widening conversions on signed types (including promotions) perform sign-extension, which does change the result if the input was negative. Having an operation whose result becomes incorrect due to integer promotion definitely falls into the “foot cannon” category. You can still feed … Read more

Is a lambda expression a legal default (non-type template) argument?

Is a lambda expression a legal default (non-type template) argument and, if so, wouldn’t this imply that each instantiation using such a default argument instantiates a unique specialization? Yes, it means that given your templated variable: template<auto l = [](){}> constexpr auto default_lambda = l; every call to default_lambda will produce a new template instantiation … Read more