c++20

C++20: Non-capturing lambda in non-type template parameter

家住魔仙堡 提交于 2020-06-25 02:41:13
问题 Does C++20 allow a non-capturing lambda decayed to a function pointer to be passed directly as a non-type template parameter? If so, what is the correct syntax? I have tried the following code in various versions of clang and gcc using -std=c++2a . #include <iostream> template<auto f> struct S { static void invoke(int x) { f(x); } }; using X = S<+[](int x) -> void { std::cout << x << " hello\n"; }>; int main() { X::invoke(42); } gcc compiles the code without complaint and the code runs as

Can I initialize an object's C style function pointer at compile time so that it calls the object's member function?

旧街凉风 提交于 2020-06-24 17:45:21
问题 I'm writing a class to wrap around a library that requires callback function pointers. See below: struct LibraryConfig { // Omitting other members... void (*callback)(const char *); }; class MyClass { private: LibraryConfig m_config; public: MyClass(const LibraryConfig &config) { // Initialize m_config using config, would like to set callback so that it calls // this->myCallback(). } void myCallback(const char *); }; Only static instances of MyClass will be declared, so construction can be

Why must the return type of a coroutine be move-constructible?

无人久伴 提交于 2020-06-23 06:49:49
问题 Consider the following code that defines the invoker class - a minimal return type for a coroutine. We explicitly delete the copy and move constructors of the invoker class. #include <coroutine> #include <cstdlib> class invoker { public: class invoker_promise { public: invoker get_return_object() { return invoker{}; } auto initial_suspend() { return std::suspend_never{}; } auto final_suspend() { return std::suspend_never{}; } void return_void() {} void unhandled_exception() { std::abort(); }

Is it possible to define a callable concept that includes functions and lambdas?

▼魔方 西西 提交于 2020-06-16 19:20:13
问题 I want to define a concept that would accept all callable objects. Here's what I have done so far: template<typename F> concept Func = std::is_function_v<std::remove_pointer_t<std::decay_t<F>>> || (requires (F f) { std::is_function_v<decltype(f.operator())>; }); bool is_callable(Func auto&&) { return true; } bool is_callable(auto&&) { return false; } Yet if I define those: auto f = [](auto a, auto b, auto c, auto d, auto e) { return a * b * c * d * e; }; int g(int a, int b) { return a + b; }

Initializing std::vector with ranges library

China☆狼群 提交于 2020-06-14 06:57:32
问题 I would like to initialize std::vector with a range of consecutive integers without typing all of them, something like a second line, which doesn't compile, in this code snippet: std::vector<int> a{0, 1, 2, 3, 4, 5}; std::vector<int> b{std::ranges::iota_view(0, 5)}; // ERROR! Of course, I would greatly prefer: std::vector<int> b{0:5}; but this is not scheduled before C++41 standard. Any ideas how to do it in C++20? 回答1: What you’re looking for is auto b=std::ranges::to<std::vector>(std:

Practical meaning of strong_ordering and weak_ordering

本小妞迷上赌 提交于 2020-06-12 02:37:25
问题 I've been reading a bit about C++20's consistent comparison (i.e. operator<=> ) but couldn't understand what's the practical difference between strong_ordering and weak_ordering (same goes for the _equality version for this manner). Other than being very descriptive about the substitutability of the type, does it actually affect the generated code? Does it add any constraints for how one could use the type? Would love to see a real-life example that demonstrates this. 回答1: Does it add any

C++20 initializing aggregates from a parenthesized list of values, not supporting inner array

孤街浪徒 提交于 2020-06-11 11:45:46
问题 C++20 adopted p0960 - allowing initialization of aggregates from a parenthesized list of values. The exact wording ([dcl.init] 17.6.2.2) says: [...] if no constructor is viable, the destination type is an aggregate class, and the initializer is a parenthesized expression-list, the object is initialized as follows. Let e 1 , …, e n be the elements of the aggregate ([dcl.init.aggr]). Let x 1 , …, x k be the elements of the expression-list. If k is greater than n, the program is ill-formed. The

C++ Concept that requires a member function with an OutputIterator as parameter

时光总嘲笑我的痴心妄想 提交于 2020-06-11 06:52:06
问题 I am playing with concepts and hit a roadblock. Or maybe it is just my mind that is blocked. I want to create a class that buffers a "bulk-readable" data source. Such a datasource should have a member function that accepts an OutputIterator and has a signature like: template<typename It> size_t read(It firstItem, size_t max) My idea was to define a BulkReadable concept similar to: template<typename Source> concept bool BulkReadable = requires(Source s, Iter out, size_t max) { {s.read(out, max

How are templates handled in C++ module system?

北慕城南 提交于 2020-06-09 11:09:37
问题 I am reading the paper A Module System for C++ to understand C++ modules, a proposed feature for C++. I am not able to fully understand how templates will be exported by this module architecture. Any ideas? 回答1: Currently C++ implementations really only have two "things" that correspond to code: source code that we human write and edit, and assembly, which the compiler spits out based on source. Because C++ templates are "reified", separate assembly is spit out for each template instantiation

How are templates handled in C++ module system?

爷,独闯天下 提交于 2020-06-09 11:07:12
问题 I am reading the paper A Module System for C++ to understand C++ modules, a proposed feature for C++. I am not able to fully understand how templates will be exported by this module architecture. Any ideas? 回答1: Currently C++ implementations really only have two "things" that correspond to code: source code that we human write and edit, and assembly, which the compiler spits out based on source. Because C++ templates are "reified", separate assembly is spit out for each template instantiation