Why does Clang 12 refuse to initialize aggregates in the C++20 way?

This is a C++20 feature that allows aggregate initialization through the standard constructor syntax, rather than the typical braced-list initialization syntax. (Note that this only works if the parameters cannot be used in a valid call to a default or copy/move constructor. If they could, that would be called instead of performing aggregate initialization.) According … Read more

asio How to change the executor inside an awaitable?

You want to associate your executor with the completion token, and then let post/dispatch/defer figure it out from there: co_await asio::post(bind_executor(pstrand, asio::use_awaitable)); See also e.g. When must you pass io_context to boost::asio::spawn? (C++) or boost::asio::bind_executor does not execute in strand for more details on how it works/why it works/when it works. It does in fact … Read more

Range TS idioms and the mysterious auto &&

This idiom is being used because the Range TS and the C++20-equivalent actually requires this idiom. The reason for this requirement has to do with the fact that proxy iterators are now legal constructs for all categories of iterators. A proxy iterator’s operator* does not return a value_type&; it returns a prvalue of an object … Read more

Contiguous iterator detection

Original answer The rationale is given in N4284, which is the adopted version of the contiguous iterators proposal: This paper introduces the term “contiguous iterator” as a refinement of random-access iterator, without introducing a corresponding contiguous_iterator_tag, which was found to break code during the Issaquah discussions of Nevin Liber’s paper N3884 “Contiguous Iterators: A Refinement … Read more

What is the purpose of C++20 std::common_reference?

common_reference came out of my efforts to come up with a conceptualization of STL’s iterators that accommodates proxy iterators. In the STL, iterators have two associated types of particular interest: reference and value_type. The former is the return type of the iterator’s operator*, and the value_type is the (non-const, non-reference) type of the elements of … Read more

Is C++20 ‘char8_t’ the same as our old ‘char’?

Disclaimer: I’m the author of the char8_t P0482 and P1423 proposals. In C++20, char8_t is a distinct type from all other types. In the related proposal for C, N2653, char8_t is a typedef of unsigned char similar to the existing typedefs for char16_t and char32_t. In C++20, char8_t has an underlying representation that matches unsigned … Read more

Why default three-way operator (spaceship ) generates equality operator (==) and user define three-way operator not?

The principle reason why equality and ordering are separated is performance. If you have a type whose ordering operations are user-defined, then more often than not, you can write a user-defined equality test operation that is more efficient at doing equality tests. And therefore, the language should encourage you to write it by not using … Read more

In which access control context are concepts evaluated?

First, let’s start with requires expressions. The section on the behavior of a requires expression has nothing special to say about the access controls of its component expressions. Similarly, the section on access controls says nothing in particular about requires expressions. In particular, there is [class.access]/4, which says: Access control is applied uniformly to all … Read more

Comparing 3 modern c++ ways to convert integral values to strings

Question 1. Why is the string stream method consistently the worst? The classical mistake: creating a new stringstream every single time template<typename T> // 1. Using stringstream string StringFromIntegral_SS(T const &value) { thread_local stringstream ss; ss.str(“”); ss.clear(); ss << value; return ss.str(); } Question 2. Why is lexical cast consistently the best? Can we assume … Read more

‘auto’ as a template argument placeholder for a function parameter

This syntax is valid in the C++ Concepts Technical Specification, but not in C++20. In C++20 concepts, auto is only permitted at the top level in a function parameter type. The relevant rule is [dcl.spec.auto] paragraph 2: A placeholder-type-specifier of the form type-constraint[opt] auto can be used as a decl-specifier of the decl-specifier-seq of a … Read more