c++20

User-Defined Deduction Guides in C++20

十年热恋 提交于 2021-02-05 05:57:18
问题 I'm working with std::variant and std::visit using the "overload" pattern that looks like this: #include <iostream> #include <variant> template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; int main(void) { std::variant<int, float> var; auto fs = overloaded { [](int var) {std::cout << "var is int" << std::endl;}, [](float var) {std::cout << "var is float" << std::endl;} }; var = 0; std::visit(fs, var); var =

Call less constrained functionally equivalent function

安稳与你 提交于 2021-02-04 16:23:07
问题 Consider the following code: #include <iostream> #include <type_traits> struct A; template<class T> concept HasParent = std::is_convertible_v<typename T::parent*, A*>; struct A{}; struct B : A { using parent = A; }; template<class T> int foo(T*) { return 1; } template<HasParent T> int foo(T*) { // call the other one? return 2; } int main() { B b; std::cout << foo(&b) << std::endl; // displays 2 return 0; } Is it possible to call the general foo<T>(T*) function from foo<HasParent T>(T*) ?

Call less constrained functionally equivalent function

非 Y 不嫁゛ 提交于 2021-02-04 16:22:50
问题 Consider the following code: #include <iostream> #include <type_traits> struct A; template<class T> concept HasParent = std::is_convertible_v<typename T::parent*, A*>; struct A{}; struct B : A { using parent = A; }; template<class T> int foo(T*) { return 1; } template<HasParent T> int foo(T*) { // call the other one? return 2; } int main() { B b; std::cout << foo(&b) << std::endl; // displays 2 return 0; } Is it possible to call the general foo<T>(T*) function from foo<HasParent T>(T*) ?

Making python generator via c++20 coroutines

…衆ロ難τιáo~ 提交于 2021-02-04 13:00:07
问题 Let's say I have this python code: def double_inputs(): while True: x = yield yield x * 2 gen = double_inputs() next(gen) print(gen.send(1)) It prints "2", just as expected. I can make a generator in c++20 like that: #include <coroutine> template <class T> struct generator { struct promise_type; using coro_handle = std::coroutine_handle<promise_type>; struct promise_type { T current_value; auto get_return_object() { return generator{coro_handle::from_promise(*this)}; } auto initial_suspend()

why a constexpr expression of literal class type with a pointer subobject can't be a non-type template argument

拟墨画扇 提交于 2021-02-04 06:32:38
问题 After looking at the post about non-type template argument,I have a confusion for an example in that post,I cite the example here: struct VariableLengthString { const char *data_ = nullptr; constexpr VariableLengthString(const char *p) : data_(p) {} auto operator<=>(const VariableLengthString&) const = default; }; template<VariableLengthString S> int bar() { static int i = 0; return ++i; } int main() { int x = bar<"hello">(); // ERROR } The post says "the relevant wording is [temp.arg.nontype

why a constexpr expression of literal class type with a pointer subobject can't be a non-type template argument

我的梦境 提交于 2021-02-04 06:30:26
问题 After looking at the post about non-type template argument,I have a confusion for an example in that post,I cite the example here: struct VariableLengthString { const char *data_ = nullptr; constexpr VariableLengthString(const char *p) : data_(p) {} auto operator<=>(const VariableLengthString&) const = default; }; template<VariableLengthString S> int bar() { static int i = 0; return ++i; } int main() { int x = bar<"hello">(); // ERROR } The post says "the relevant wording is [temp.arg.nontype

-9'223'372'036'854'775'808LL is unsigned

时间秒杀一切 提交于 2021-02-04 05:56:50
问题 Since C++20 two's complement representation is the only representation allowed by the standard, with the guaranteed range from -2 N-1 to +2 N-1 -1. So for a 64-bit signed integer type the range goes from -9'223'372'036'854'775'808 to 9'223'372'036'854'775'807 . However, this code does not compile on Visual Studio (and neither on gcc) int main() { long long x{-9'223'372'036'854'775'808LL}; // error C4146: unary minus operator applied to unsigned type, result still unsigned // error C2397:

-9'223'372'036'854'775'808LL is unsigned

╄→尐↘猪︶ㄣ 提交于 2021-02-04 05:56:19
问题 Since C++20 two's complement representation is the only representation allowed by the standard, with the guaranteed range from -2 N-1 to +2 N-1 -1. So for a 64-bit signed integer type the range goes from -9'223'372'036'854'775'808 to 9'223'372'036'854'775'807 . However, this code does not compile on Visual Studio (and neither on gcc) int main() { long long x{-9'223'372'036'854'775'808LL}; // error C4146: unary minus operator applied to unsigned type, result still unsigned // error C2397:

Where to initialize static const member in c++17 or newer?

試著忘記壹切 提交于 2021-01-29 05:58:11
问题 The questions is where to initialize static const member in c++17 or newer? Please consider the following two solutions for initializing a static const member in c++: Solution 1 (for c++14 or older): //foo.h: #include <iostream> struct foo{ static const std::string ToBeInitialized; }; //foo.cpp #include <iostream> #include "foo.h" const std::string foo::ToBeInitialized{"with a value"}; Solution 2 (for c++17 or newer): //foo.h: #include <iostream> struct foo{ inline static const std::string

How can I combine everything I've written into one template without using overloads? [closed]

旧城冷巷雨未停 提交于 2021-01-28 21:56:50
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed yesterday . Improve this question How to make one template so that it can be called with several parameters, without creating unnecessary overloads with code repetition template <typename... T1> std::filesystem::path createFullPath(T1&&... components) { return (std::filesystem::path{components} / ...); } std: