c++20

Why is std::ssize() introduced in C++20?

故事扮演 提交于 2019-12-03 00:52:24
C++20 introduced the std::ssize() free function as below: template <class C> constexpr auto ssize(const C& c) -> std::common_type_t<std::ptrdiff_t, std::make_signed_t<decltype(c.size())>>; A possible implementation seems using static_cast , to convert the return value of the size() member function of cl ass C into its signed counterpart. Since the size() member function of C always returns non-negative values, why would anyone want to store them in signed variables? In case one really wants to, it is a matter of simple static_cast . Why is std::ssize() introduced in C++20? Nadav Har'El The

Why do we require requires requires?

风流意气都作罢 提交于 2019-12-02 16:32:25
One of the corners of C++20 concepts is that there are certain situations in which you have to write requires requires . For instance, this example from [expr.prim.req]/3 : A requires-expression can also be used in a requires-clause ([temp]) as a way of writing ad hoc constraints on template arguments such as the one below: template<typename T> requires requires (T x) { x + x; } T add(T a, T b) { return a + b; } The first requires introduces the requires-clause , and the second introduces the requires-expression . What is the technical reason behind needing that second requires keyword? Why

What are coroutines in C++20?

ⅰ亾dé卋堺 提交于 2019-12-02 13:52:08
What are coroutines in c++20 ? In what ways it is different from "Parallelism2" or/and "Concurrency2" (look into below image)? The below image is from ISOCPP. https://isocpp.org/files/img/wg21-timeline-2017-03.png At an abstract level, Coroutines split the idea of having an execution state off of the idea of having a thread of execution. SIMD (single instruction multiple data) has multiple "threads of execution" but only one execution state (it just works on multiple data). Arguably parallel algorithms are a bit like this, in that you have one "program" run on different data. Threading has

In which access control context are evaluated concepts?

元气小坏坏 提交于 2019-12-01 18:06:48
This question is a follow up to this one [temp.concept]/5 says: A concept is not instantiated ([temp.spec]). [ Note: An id-expression that denotes a concept specialization is evaluated as an expression ([expr.prim.id]). [...]] So maybe an expression that name a concept specialization can have different value because of accessibility. If it were the case, I wonder in which context would be evaluated the expression: The context of the concept definition; The context of the expression; The context of the expression recursively applied to concepts expression appearing in concepts definition? For

In which access control context are evaluated concepts?

♀尐吖头ヾ 提交于 2019-12-01 16:17:04
问题 This question is a follow up to this one [temp.concept]/5 says: A concept is not instantiated ([temp.spec]). [ Note: An id-expression that denotes a concept specialization is evaluated as an expression ([expr.prim.id]). [...]] So maybe an expression that name a concept specialization can have different value because of accessibility. If it were the case, I wonder in which context would be evaluated the expression: The context of the concept definition; The context of the expression; The

Using template parameter in a generic lambda

不打扰是莪最后的温柔 提交于 2019-12-01 05:25:06
GCC allows the following syntax as an extension: // a functional object that will add two like-type objects auto add = [] <typename T> (T a, T b) { return a + b; }; In n3418 , the 2012 proposal for generic lambdas, we see syntax that allows the above: overload( []<class T>(T* p) {...}, However, since it is an extension the syntax is clearly absent (or not allowed.) In what situations would the above be useful when we have auto, and why is the syntax absent (or not allowed)? It seems to me that C++14's polymorphic lambdas are just more terse. You can reproduce the effect of your sample

Why do we need the spaceship <=> operator in C++?

可紊 提交于 2019-12-01 03:39:26
Why do we need such an operator in C++ and how is it useful in modern C++ programming? Any real world code examples where this can be applied will help. This question is geared to understand the practical application in real world without reading wordy proposal from Herb Sutter. No offense to the proposal though. I'll give you three points of motivation, just off the top of my head: It's the common generalization of all other comparison operator (for totally-ordered domains): > , >= , == , <= , < . Using <=> (spaceship), you can implement each of these other operations in a completely generic

Why will std::rel_ops::operators be deprecated in C++20?

懵懂的女人 提交于 2019-12-01 02:25:26
According to cppreference.com , std::rel_ops::operator!=,>,<=,>= will be deprecated in C++20. What's the rationale behind? In C++20, you get three-way comparison (operator <=> ), which automatically "generates" default comparisons if provided: struct A { // You only need to implement a single operator. std::strong_ordering operator<=>(const A&) const; }; // Compiler generates all 6 relational operators A to1, to2; if (to1 == to2) { /* ... */ } // ok if (to1 <= to2) { /* ... */ } // ok, single call to <=> There are multiple advantages of the three-way comparison over std::rel_ops , which is

Using template parameter in a generic lambda

无人久伴 提交于 2019-12-01 02:09:40
问题 GCC allows the following syntax as an extension: // a functional object that will add two like-type objects auto add = [] <typename T> (T a, T b) { return a + b; }; In n3418, the 2012 proposal for generic lambdas, we see syntax that allows the above: overload( []<class T>(T* p) {...}, However, since it is an extension the syntax is clearly absent (or not allowed.) In what situations would the above be useful when we have auto, and why is the syntax absent (or not allowed)? 回答1: It seems to me

What is consteval?

*爱你&永不变心* 提交于 2019-12-01 02:06:11
Apparently, consteval is going to be a keyword in C++20. The cppreference page for it is currently blank. What is it going to be and how does it relate to constexpr ? T.C. It declares immediate functions , that is, functions that must be evaluated at compile time to produce a constant. (It used to be spelled constexpr! in a previous revision of the paper.) In contrast, constexpr functions may be evaluated at compile time or run time, and need not produce a constant in all cases. The adopted paper is P1073R3 , which is not yet publicly available, but a previous revision is available and the