c++20

Can class template constructors have a redundant template parameter list in c++20

雨燕双飞 提交于 2020-08-27 21:35:39
问题 As far as I'm aware, the following code: template<typename T> struct S { S<T>(); }; is well-formed, even though the <T> in the declaration of the constructor is redundant. However, on gcc trunk (but not on gcc10.2), with -std=c++20 this gives an error: error: expected unqualified-id before ')' token 3 | S<T>(); ^ The code compiles on clang trunk with -std=c++20 . Is this a bug, or is this a breaking change in c++20 that is yet to be implemented in all compilers? 回答1: There was a change, in

How can I combine several return type requirements of C++20 constraints into one return type requirement?

荒凉一梦 提交于 2020-08-24 05:48:25
问题 Currently, I have implemented the Allocator concept (which refers to the Boost proposal) using C++20 constraints and concepts: #include <concepts> #include <iterator> template <class A> concept allocator = std::copy_constructible<A> && std::equality_comparable<A> && requires(A a) { { a.allocate(0) } -> std::regular; { a.allocate(0) } -> std::constructible_from<std::nullptr_t>; { a.allocate(0) } -> std::equality_comparable_with<std::nullptr_t>; { a.allocate(0) } -> std::random_access_iterator;

How can I combine several return type requirements of C++20 constraints into one return type requirement?

穿精又带淫゛_ 提交于 2020-08-24 05:46:06
问题 Currently, I have implemented the Allocator concept (which refers to the Boost proposal) using C++20 constraints and concepts: #include <concepts> #include <iterator> template <class A> concept allocator = std::copy_constructible<A> && std::equality_comparable<A> && requires(A a) { { a.allocate(0) } -> std::regular; { a.allocate(0) } -> std::constructible_from<std::nullptr_t>; { a.allocate(0) } -> std::equality_comparable_with<std::nullptr_t>; { a.allocate(0) } -> std::random_access_iterator;

What is the fastest way to check the leading characters in a char array?

瘦欲@ 提交于 2020-08-24 05:10:52
问题 I reached a bottleneck in my code, so the main issue of this question is performance. I have a hexadecimal checksum and I want to check the leading zeros of an array of chars. This is what I am doing: bool starts_with (char* cksum_hex, int n_zero) { bool flag {true}; for (int i=0; i<n_zero; ++i) flag &= (cksum_hex[i]=='0'); return flag; } The above function returns true if the cksum_hex has n_zero leading zeros. However, for my application, this function is very expensive (60% of total time).

Is using malloc for int undefined behavior until C++20

戏子无情 提交于 2020-08-21 03:40:04
问题 I was told that the following code has undefined behavior until C++20: int *p = (int*)malloc(sizeof(int)); *p = 10; Is that true? The argument was that the lifetime of the int object is not started before assigning the value to it (P0593R6). To fix the problem, placement new should be used: int *p = (int*)malloc(sizeof(int)); new (p) int; *p = 10; Do we really have to call a default constructor that is trivial to start the lifetime of the object? At the same time, the code does not have

Is using malloc for int undefined behavior until C++20

自古美人都是妖i 提交于 2020-08-21 03:40:03
问题 I was told that the following code has undefined behavior until C++20: int *p = (int*)malloc(sizeof(int)); *p = 10; Is that true? The argument was that the lifetime of the int object is not started before assigning the value to it (P0593R6). To fix the problem, placement new should be used: int *p = (int*)malloc(sizeof(int)); new (p) int; *p = 10; Do we really have to call a default constructor that is trivial to start the lifetime of the object? At the same time, the code does not have

How can unspecified types be used in C++20 'requires' expressions?

ぐ巨炮叔叔 提交于 2020-08-20 11:29:32
问题 I'm trying to write a C++20 concept to express the requirement that a type have a certain method, which takes an argument, but for the purposes of this concept I don't care what the argument type is. I've tried to write something like: template <typename T> concept HasFooMethod = requires(T t, auto x) { { t.Foo(x) } -> std::same_as<void>; }; however, both gcc and clang reject this, giving an error that 'auto' cannot be used in the parameter list of a requires expression this way. An

Why can I invoke == with a defaulted <=> but not a user-provided one?

风格不统一 提交于 2020-08-19 04:27:10
问题 #include <compare> struct A { int n; auto operator <=>(const A&) const noexcept = default; }; struct B { int n; auto operator <=>(const B& rhs) const noexcept { return n <=> rhs.n; } }; int main() { A{} == A{}; // ok B{} == B{}; // error: invalid operands to binary expression } compiled with clang-10 as clang -std=c++20 -stdlib=libc++ main.cpp Why does A{} == A{} work but not B{} == B{} ? 回答1: In the original design of the spaceship operator, == is allowed to call <=> , but this is later

Why can I invoke == with a defaulted <=> but not a user-provided one?

☆樱花仙子☆ 提交于 2020-08-19 04:25:08
问题 #include <compare> struct A { int n; auto operator <=>(const A&) const noexcept = default; }; struct B { int n; auto operator <=>(const B& rhs) const noexcept { return n <=> rhs.n; } }; int main() { A{} == A{}; // ok B{} == B{}; // error: invalid operands to binary expression } compiled with clang-10 as clang -std=c++20 -stdlib=libc++ main.cpp Why does A{} == A{} work but not B{} == B{} ? 回答1: In the original design of the spaceship operator, == is allowed to call <=> , but this is later