c++20

What trait / concept can guarantee memsetting an object is well defined?

心已入冬 提交于 2019-11-29 16:37:22
问题 Let's say I have defined a zero_initialize() function: template<class T> T zero_initialize() { T result; std::memset(&result, 0, sizeof(result)); return result; } // usage: auto data = zero_initialize<Data>(); Calling zero_initialize() for some types would lead to undefined behavior 1, 2 . I'm currently enforcing T to verify std::is_pod. With that trait being deprecated in C++20 and the coming of concepts, I'm curious how zero_initialize() should evolve. What (minimal) trait / concept can

How and why did the ISO C++ Standards Committee (WG21) decide to accept the proposal for a spaceship operator as it is? [closed]

ぃ、小莉子 提交于 2019-11-29 15:17:56
问题 In a recently published paper [1], Herb Sutter et al. describe an extension of the programming language C++ by a three way comparison operator. The authors refer to a considerable number of earlier proposals, all of which were ultimately rejected. The clever core concept of the new approach is to encode different categories of relations in the return type of the comparison operator. The authors declare that The primary design goal is conceptual integrity [Brooks 1975], which means that the

Range TS idioms and the mysterious auto &&

筅森魡賤 提交于 2019-11-29 13:59:06
In pre-Range TS code, I might do something like the following to get a (potentially modifiable) reference to a ForwardIterator's value: auto &val = *it; This would also be used in range-based for loops over such iterators: for(auto &val : some_range) However, in C++20 and Range TS-based code, I'm seeing a lot of auto&& usage in these locations. I understand what auto&& is doing from a language standpoint. What I don't understand is why it's being used in these places, when auto& ought to work just fine? Most code of this nature isn't forwarding the reference, so why is it using a forwarding

Can a non-type template parameter be of type “void*”?

五迷三道 提交于 2019-11-29 13:37:28
Yakk - Adam Nevraumont said : Non-type template parameters of type void* are not allowed in at least some versions of the standard. Is this true? If it is true, in which versions of the standard are non-type template parameters of type void* not allowed? (Note: as noted in a comment to answer another comment , this is about non-type template parameters , not template type arguments , which can be any valid type-id per [temp.arg.type] , including void* . TL;DR Template parameters of type void* are valid since C++20. They are invalid prior to C++20. C++20 C++20 relaxed the restrictions on the

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

冷暖自知 提交于 2019-11-29 04:53:58
问题 In the CPP reference documentation, I noticed for char The character types are large enough to represent any UTF-8 eight-bit code unit (since C++14) and for char8_t type for UTF-8 character representation, required to be large enough to represent any UTF-8 code unit (8 bits) Does that mean both are the same type? Or does char8_t have some other feature? 回答1: char8_t is not the same as char . It behaves exactly the same as unsigned char though per [basic.fundamental]/9 Type char8_­t denotes a

C++20 bit_cast vs reinterpret_cast

雨燕双飞 提交于 2019-11-29 02:32:56
问题 According to the last meeting of the ISO C++ Committee, bit-cast will be introduced in C++20 standard. I know that reinterpret_cast is not suitable for this job due to type aliasing rules but my question is why did they choose not to extend the reinterpret_cast to treat the object like it bit sequence representation and preferred to give this functionality as a new language construct? 回答1: Well, there is one obvious reason: because it wouldn't do everything that bit_cast does. Even in the C+

What are customization point objects and how to use them?

人盡茶涼 提交于 2019-11-29 02:14:33
问题 The last draft of the c++ standard introduces the so-called "customization point objects" ([customization.point.object]), which are widely used by the ranges library. I seem to understand that they provide a way to write custom version of begin , swap , data , and the like, which are found by the standard library by ADL. Is that correct? How is this different from previous practice where a user defines an overload for e.g. begin for her type in her own namespace? In particular, why are they

How should I write my C++ to be prepared for C++ modules?

余生颓废 提交于 2019-11-28 17:19:59
问题 There are already two compilers that support C++ modules: Clang: http://clang.llvm.org/docs/Modules.html MS VS 2015: http://blogs.msdn.com/b/vcblog/archive/2015/12/03/c-modules-in-vs-2015-update-1.aspx When starting a new project now, what should I pay attention to in order to be able to adopt the modules feature when it is eventually released in my compiler? Is it possible to use modules and still maintain compatibility with older compilers that do not support it? 回答1: There are already two

Can I take the address of a function defined in standard library?

拜拜、爱过 提交于 2019-11-28 08:58:04
Consider the following code: #include <cctype> #include <functional> #include <iostream> int main() { std::invoke(std::boolalpha, std::cout); // #1 using ctype_func = int(*)(int); char c = std::invoke(static_cast<ctype_func>(std::tolower), 'A'); // #2 std::cout << c << "\n"; } Here, the two calls to std::invoke are labeled for future reference. The expected output is: a Is the expected output guaranteed in C++20? (Note: there are two functions called tolower — one in <cctype> and the other in <locale> . The explicit cast is introduced to select the desired overload.) Short answer No.

Range TS idioms and the mysterious auto &&

一笑奈何 提交于 2019-11-28 07:21:48
问题 In pre-Range TS code, I might do something like the following to get a (potentially modifiable) reference to a ForwardIterator's value: auto &val = *it; This would also be used in range-based for loops over such iterators: for(auto &val : some_range) However, in C++20 and Range TS-based code, I'm seeing a lot of auto&& usage in these locations. I understand what auto&& is doing from a language standpoint. What I don't understand is why it's being used in these places, when auto& ought to work