c++20

How you create your own views that interact with existing views with operator |?

穿精又带淫゛_ 提交于 2021-02-16 04:36:33
问题 Why does this code work with the #if 0 block in place, but fails with a fairly complex set of error messages if you remove it? And more importantly, how do I make it the same result as the very similar block above it? #include <ranges> #include <iterator> #include <optional> #include <string_view> #include <iostream> #include <algorithm> template <::std::ranges::view View, typename Pred> requires ::std::ranges::input_range<View> && ::std::ranges::common_range<View> && ::std::is_object_v<Pred>

How you create your own views that interact with existing views with operator |?

妖精的绣舞 提交于 2021-02-16 04:35:41
问题 Why does this code work with the #if 0 block in place, but fails with a fairly complex set of error messages if you remove it? And more importantly, how do I make it the same result as the very similar block above it? #include <ranges> #include <iterator> #include <optional> #include <string_view> #include <iostream> #include <algorithm> template <::std::ranges::view View, typename Pred> requires ::std::ranges::input_range<View> && ::std::ranges::common_range<View> && ::std::is_object_v<Pred>

Check existence of (global) function but disallow implicit conversion

随声附和 提交于 2021-02-11 17:08:12
问题 Consider this simple check for whether a (global) function is defined: template <typename T> concept has_f = requires ( const T& t ) { Function( t ); }; // later use in MyClass<T>: if constexpr ( has_f<T> ) Function( value ); unfortunately this allows for implicit conversions . This is obviously a big risk for mess-ups. Question: How to check if Function( const T& t ) 'explicitly' exists? Something like if constexpr ( std::is_same_v<decltype( Function( t ) ), void> ) should be free of implict

Check existence of (global) function but disallow implicit conversion

血红的双手。 提交于 2021-02-11 17:06:45
问题 Consider this simple check for whether a (global) function is defined: template <typename T> concept has_f = requires ( const T& t ) { Function( t ); }; // later use in MyClass<T>: if constexpr ( has_f<T> ) Function( value ); unfortunately this allows for implicit conversions . This is obviously a big risk for mess-ups. Question: How to check if Function( const T& t ) 'explicitly' exists? Something like if constexpr ( std::is_same_v<decltype( Function( t ) ), void> ) should be free of implict

Smart pointer toolkit using variadic CRTP

只愿长相守 提交于 2021-02-11 12:52:28
问题 I am about to design and implement a kind of smart pointer toolkit - a set of classes to define various types of smart pointers like unique_ptr, intrusive_ptr, shared_ptr, observing_ptr, tagged_ptr etc. Just to mention I am working in freestanding environment where I have no c++ library available. My intersion is to avoid code duplications, make it follow an elegant design principle. Let me describe my thoughts in there. Design Considerations: I wanna use variadic CRTP approach to mixin the

How to obtain constexpr `.size()` of a non-static std::array member

社会主义新天地 提交于 2021-02-11 12:41:55
问题 Given that std::array<T,N>::size is constexpr, in the snippet below Why does it matter that Foo1::u is not a static member? The type is known at compile time and so is its size() . What's wrong with Foo2::bigger() ? Listing: // x86-64 gcc 10.1 // -O3 --std=c++20 -pedantic -Wall -Werror #include <array> #include <cstdint> union MyUnion { std::array<uint8_t,32> bytes; std::array<uint32_t,8> words; }; struct Foo1 { MyUnion u; static constexpr size_t length {u.bytes.size()}; //invalid use of non

Clang: gnu standard library requires -fcoroutines but clang only supports -fcoroutines-ts

半世苍凉 提交于 2021-02-10 14:23:54
问题 It seems like the clang compiler flags are currently not compatible with the gnu standard library. Is there still a way to use coroutines? 回答1: It's not quite accurate to say that GCC's header requires -fcoroutines , what it actually requires is for the __cpp_impl_coroutine macro to be defined by the compiler, indicating that the compiler supports C++20 Coroutines. With GCC, you activate that support by using -fcoroutines . 来源: https://stackoverflow.com/questions/64525567/clang-gnu-standard

Clang: gnu standard library requires -fcoroutines but clang only supports -fcoroutines-ts

对着背影说爱祢 提交于 2021-02-10 14:23:01
问题 It seems like the clang compiler flags are currently not compatible with the gnu standard library. Is there still a way to use coroutines? 回答1: It's not quite accurate to say that GCC's header requires -fcoroutines , what it actually requires is for the __cpp_impl_coroutine macro to be defined by the compiler, indicating that the compiler supports C++20 Coroutines. With GCC, you activate that support by using -fcoroutines . 来源: https://stackoverflow.com/questions/64525567/clang-gnu-standard

How do I check if ranges:: algorithms like find_if returned a value?

元气小坏坏 提交于 2021-02-10 06:43:49
问题 For example, if I want to find the smallest element of a collection, but only the smallest even element, I'd like to call ranges::min_element with a filtered range like so: using ranges::views::filter; using ranges::min_element; std::vector<int> vals{1,2,3,4,5}; auto minVal = min_element(vals | filter([](int next){return next % 2 == 0;})); How do I check if the returned range is empty, and if not, access the value? The same applies to other range algorithms like ranges::find , ranges::find_if

“requires” ignores a field is not static

我们两清 提交于 2021-02-08 19:47:11
问题 Consider the following code: #include <iostream> constexpr int fun(int const&) { return 5; } struct T { int x; }; int main() { std::cout << fun(T::x) << std::endl; // Line A std::cout << requires { fun(T::x); } << std::endl; // Line B } If I only comment Line B, then the code cannot be compiled (as expected, since x is not static in T ). But when I only comment Line A, the code compiles just fine with both Clang 11.0.0 and GCC 10.2.0 (both output 1 ). What it is that I am missing about