c++20

Why does `auto` not adopt the constexpr'ness of its initializing expression?

孤人 提交于 2021-02-20 04:23:05
问题 Why doesn't defining a variable with auto keyword carry the constexpr 'ness of the expression used to initialize it? As an example, consider the following code: #include <string_view> constexpr std::string_view f() { return "hello"; } static constexpr std::string_view g() { constexpr auto x = f(); // (*) return x.substr(1, 3); } int foo() { return g().length(); } With GCC 10.2 and --std=c++20 -fsanitize=undefined -O3 , this compiles into: foo(): mov eax, 3 ret But if we remove the constexpr

A variable template that is true iff a class template would instantiate?

醉酒当歌 提交于 2021-02-19 05:14:47
问题 Let's say I have a class template A that has one type template parameter, and a single primary specialization: template<typename T> struct A { /*...*/ }; For some T arguments A<T> will instantiate successfully, for others it won't. Without modifying or extending the definition of A , is it possible to write a bool variable template: template<typename T> constexpr bool WorksWithA = /*...*/; such that WorkWithA<T> is true iff A<T> would instantiate successfully? Update Posted this as separate

A variable template that is true iff a class template would instantiate?

安稳与你 提交于 2021-02-19 05:14:19
问题 Let's say I have a class template A that has one type template parameter, and a single primary specialization: template<typename T> struct A { /*...*/ }; For some T arguments A<T> will instantiate successfully, for others it won't. Without modifying or extending the definition of A , is it possible to write a bool variable template: template<typename T> constexpr bool WorksWithA = /*...*/; such that WorkWithA<T> is true iff A<T> would instantiate successfully? Update Posted this as separate

What's the relationship between C++ “concept” and duck typing?

风流意气都作罢 提交于 2021-02-18 22:10:28
问题 There was an earlier question (8 years ago!) about the relationship between templates and duck typing here: What's the relationship between C++ template and duck typing? I've borrowed and modified the tag line for my question on a new feature of C++. With C++20 there will be the new feature of "concept" that looks much more like a duck-typing feature. Is it correct that the new C++ "concept" is equivalent to duck typing for C++? If not, how is it different? 回答1: With C++20 there will be the

If there's an if-constexpr, how come there's no switch-constexpr?

时光怂恿深爱的人放手 提交于 2021-02-18 11:17:04
问题 In C++17, if constexpr was introduced; however, there doesn't seem to be a switch constexpr (see here). Why is that? That is, if a compiler supports if constexpr , is it not also trivial for it to support switch constexpr (at worst as an if-then-else-if-etc. chain, or multiple if's with some flags to control fallthrough)? 回答1: if constexpr was ultimately derived from a more sane form of the static if concept. Because of that derivation, applying the same idea to switch does not appear to have

Can C++20 coroutines be copied?

流过昼夜 提交于 2021-02-16 18:07:09
问题 I've been playing around with C++20 coroutines and trying to move some of my codebase over to use them. I've run into an issue, though, as it doesn't seem that the new coroutines can be copied. The generator objects have deleted copy-constructors and copy-assignment operators, and nothing I've looked into has seemed to have a way. Can this be done? For reference, I have written a little test program with a failing attempt at copying C++20 coroutines as well as a successful attempt to do the

Can C++20 coroutines be copied?

元气小坏坏 提交于 2021-02-16 18:02:01
问题 I've been playing around with C++20 coroutines and trying to move some of my codebase over to use them. I've run into an issue, though, as it doesn't seem that the new coroutines can be copied. The generator objects have deleted copy-constructors and copy-assignment operators, and nothing I've looked into has seemed to have a way. Can this be done? For reference, I have written a little test program with a failing attempt at copying C++20 coroutines as well as a successful attempt to do the

Disable non-templated methods with concepts

落花浮王杯 提交于 2021-02-16 17:58:25
问题 Is there a syntax to constraint a non-templated method? All the syntaxes I've tried on godbolt with clang concepts branch and gcc fail to compile: // these examples do not compile template <bool B> struct X { requires B void foo() {} }; template <class T> struct Y { requires (std::is_trivially_copyable_v<T>) auto foo() {} }; The trick to make it compile is the same trick you needed to do with SFINAE, make the methods template, even though they really are not templates. And funny enough, the

Why C++ ranges “transform -> filter” calls transform twice for values that match the filter's predicate?

末鹿安然 提交于 2021-02-16 11:53:12
问题 Consider the following code using the ranges library (from c++20) #include <iostream> #include <ranges> #include <vector> int main() { std::vector<int> inputs{1, 2, 3, 4, 5, 6}; auto square_it = [](auto i) { std::cout << i << std::endl; return i * 2; }; auto results = inputs | std::views::transform(square_it) | std::views::filter([](auto i){ return i % 3 == 0; }); for(auto r : results) { // std::cout << r << std::endl; } } The cout in the square function is to log when the square function is

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

孤街醉人 提交于 2021-02-16 04:38:54
问题 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>