c++20

Why does a consteval function allow undefined behavior?

北慕城南 提交于 2019-12-03 23:35:35
问题 There is a very neat property of constant expressions in C++: their evaluation cannot have undefined behavior (7.7.4.7): An expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine ([intro.execution]), would evaluate one of the following: ... an operation that would have undefined behavior as specified in [intro] through [cpp] of this document [ Note: including, for example, signed integer overflow ([expr.prop]), certain pointer

Is compiler allowed to call an immediate (consteval) function during runtime?

好久不见. 提交于 2019-12-03 16:34:34
This might be a stupid question, but I am confused. I had a feeling that an immediate ( consteval ) function has to be executed during compile time and we simply cannot see its body in the binary. This article clearly supports my feeling: This has the implication that the [immediate] function is only seen at compile time. Symbols are not emitted for the function, you cannot take the address of such a function, and tools such as debuggers will not be able to show them. In this matter, immediate functions are similar to macros. The similar strong claim might be found in Herb Sutter's publication

What does “compares less than 0” mean?

本小妞迷上赌 提交于 2019-12-03 14:45:56
Context While I was reading Consistent comparison , I have noticed a peculiar usage of the verb to compare : There’s a new three-way comparison operator, <=> . The expression a <=> b returns an object that compares <0 if a < b , compares >0 if a > b , and compares ==0 if a and b are equal/equivalent. Another example found on the internet (emphasis mine): It returns a value that compares less than zero on failure. Otherwise, the returned value can be used as the first argument on a later call to get. One last example , found in a on GitHub (emphasis mine): // Perform a circular 16 bit compare.

C++2a contract programming and compilers

无人久伴 提交于 2019-12-03 13:22:31
I'm interested in studying the recently accepted contract programming for C++20 for learning and investigation purpose. As I'm looking around for compiler support, I'm disappointed to not find any. Both gcc and clang are quite clear they do not support this feature within their --std=c++2a mode. Since the approval is pretty recent, I'm not too surprised that current compilers do not support the exact semantic proposed. What is more surprising to me though is that there is absolutely nothing, not even a compiler-specific extension which would mimic, even in a limited way, the same feature. I

Lambda closure type constructors

ぐ巨炮叔叔 提交于 2019-12-03 12:42:01
The cppreference shows that there are different rules for lambda closure type constructors. Default Construction - Until C++14 ClosureType() = delete; (until C++14) Closure types are not Default Constructible. Closure types have a deleted (until C++14)no (since C++14) default constructor. Default Construction - Since C++14 Closure types have no (since C++14) default constructor. Default Construction - Since C++20 If no captures are specified, the closure type has a defaulted default constructor. Otherwise, it has no default constructor (this includes the case when there is a capture-default,

How to use C++20's likely/unlikely attribute in if-else statement

我与影子孤独终老i 提交于 2019-12-03 10:27:25
This question is about C++20's [[likely]] / [[unlikely]] feature, not compiler-defined macros. This documents ( cppreference ) only gave an example on applying them to a switch-case statement. This switch-case example compiles perfectly with my compiler (g++-7.2) so I assume the compiler has implemented this feature, though it's not yet officially introduced in current C++ standards. But when I use them like this: if (condition) [[likely]] { ... } else { ... } , I got a warning: "warning: attributes at the beginning of statement are ignored [-Wattributes]". So how should I use these attributes

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

筅森魡賤 提交于 2019-12-03 10:20:59
问题 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

Why use std::forward in concepts?

老子叫甜甜 提交于 2019-12-03 04:39:34
I was reading the cppreference page on Constraints and noticed this example: // example constraint from the standard library (ranges TS) template <class T, class U = T> concept bool Swappable = requires(T t, U u) { swap(std::forward<T>(t), std::forward<U>(u)); swap(std::forward<U>(u), std::forward<T>(t)); }; I'm puzzled why they're using std::forward . Some attempt to support reference types in the template parameters? Don't we want to call swap with lvalues, and wouldn't the forward expressions be rvalues when T and U are scalar (non-reference) types? For example, I would expect this program

What are coroutines in C++20?

帅比萌擦擦* 提交于 2019-12-03 01:33:44
问题 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 回答1: 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

Why do we require requires requires?

我的梦境 提交于 2019-12-03 01:15:41
问题 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