c++20

c++ generic compile-time for loop

做~自己de王妃 提交于 2019-11-30 23:17:50
问题 In some contexts, it could be useful/necessary to have a for loop evaluated/unrolled at compile time. For example, to iterate over the elements of a tuple , one needs to use std::get<I> , which depends on a template int parameter I , hence it has to be evaluated at compile time. Using compile recursion one can solve a specific problem, as for instance discussed here, here, and, specifically for std::tuple here. I am interested, however, on how to implement a generic compile-time for loop. The

Lambda functions with template parameters, not in function parameters

折月煮酒 提交于 2019-11-30 13:52:06
Why does the first call not compile? auto get1 = []<int B>() { return B; }; auto get2 = []<typename B>(B b) { return b; }; int main() { get1<5>(); // error: no match for operator< get2(5); // ok } The reason I use this, is an expression repeated many times in code. Of course I can use a real function template, but just I am curious WHY. Barry This is easier to understand if you consider what the equivalent class type looks like to your get1 : struct get1_t { template <int B> operator()() const { return B; } }; get1_t get1; get1<5>(); // error You're trying to provide an explicit template

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

北城余情 提交于 2019-11-30 11:03:01
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 guarantee memsetting an object is well defined? Should I use std::uninitialized_fill instead of std:

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-30 10:15:28
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 design is coherent and reliably does what the user expects it to do. A total of five categories of

C++20 bit_cast vs reinterpret_cast

人盡茶涼 提交于 2019-11-30 05:01:32
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? Well, there is one obvious reason: because it wouldn't do everything that bit_cast does. Even in the C++20 world where we can allocate memory at compile time, reinterpret_cast is forbidden in constexpr functions.

What are customization point objects and how to use them?

試著忘記壹切 提交于 2019-11-30 04:47:10
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 objects ? What are customization point objects? They are function object instances in namespace std

Is it possible to ensure a constexpr function is called at most once at compile time?

感情迁移 提交于 2019-11-29 22:40:13
问题 As the title asks: Is it possible to ensure a constexpr function is called at most once at compile time? This clearly won't be possible if the function is not constepxr; I could write a function that gets called whenever I press the space bar, so the compiler could never figure that out at compile time. 回答1: Short answer: no, because constexpr functions cannot read/set external state. (They can have internal state, but they still need to be "pure") . Real answer: probably yes, but it's a bad

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

旧街凉风 提交于 2019-11-29 21:14:31
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? There are already two compilers that support C++ modules clang: http://clang.llvm.org/docs/Modules.html MS VS 2015: http://blogs

Lambda functions with template parameters, not in function parameters

瘦欲@ 提交于 2019-11-29 19:09:35
问题 Why does the first call not compile? auto get1 = []<int B>() { return B; }; auto get2 = []<typename B>(B b) { return b; }; int main() { get1<5>(); // error: no match for operator< get2(5); // ok } The reason I use this, is an expression repeated many times in code. Of course I can use a real function template, but just I am curious WHY. 回答1: This is easier to understand if you consider what the equivalent class type looks like to your get1 : struct get1_t { template <int B> operator()() const

Does C++ standard library provide more compact and generalized version of the erase–remove idiom?

你说的曾经没有我的故事 提交于 2019-11-29 17:16:25
问题 We can erase one element/ entry from a container by the popular erase–remove idiom. However, many of us would have encountered some problems while applying this idiom: one can easily get into the pitfall of typos like c.erase(std::remove_if(c.begin(), c.end(), pred)); // , c.end() //---> missing here or c.erase((std::remove_if(c.begin(), c.end(), pred), c.end())) // ^^ ^^ // extra () makes it pass only c.end() to the c.erase It even follows the wrong semantics for containers like std::list by