c++20

Will consteval allow using static_assert on function arguments?

旧街凉风 提交于 2020-01-22 18:24:06
问题 Currently you cannot use static_assert to verify parameters of a constexpr function, even if all calls to it are indeed constexpr . That makes sense because the compiler still has to create a non-constexpr instantiation of this function in case some other module will try to call it. Sadly, this is the case even if the function is static or in an anonymous namespace. C++20 however, will introduce a new keyword consteval which is like constexpr but it doesn't allow calling a function in a non

if constexpr - why is discarded statement fully checked?

一笑奈何 提交于 2020-01-22 17:37:48
问题 I was messing around with c++20 consteval in GCC 10 and wrote this code #include <optional> #include <tuple> #include <iostream> template <std::size_t N, typename Predicate, typename Tuple> consteval std::optional<std::size_t> find_if_impl(Predicate&& pred, Tuple&& t) noexcept { constexpr std::size_t I = std::tuple_size_v<std::decay_t<decltype(t)>> - N; if constexpr (N == 0u) { return std::nullopt; } else { return pred(std::get<I>(t)) ? std::make_optional(I) : find_if_impl<N - 1u>(std:

Use of variable in own initializer

余生长醉 提交于 2020-01-22 17:07:05
问题 [basic.scope.pdecl]/1 of the C++20 standard draft had the following (non-normative) example in a note (partial quote from before the merge of pull request 3580, see answer to this question): unsigned char x = x; [...] x is initialized with its own (indeterminate) value. Does this actually have well-defined behavior in C++20? Generally the self-initialization of the form T x = x; has undefined behavior by virtue of x 's value being indeterminate before initialization is completed. Evaluating

Using (draft) C++20 chrono, how to compute various facts about a date

情到浓时终转凉″ 提交于 2020-01-22 06:59:05
问题 https://www.timeanddate.com/date/weekday.html computes various facts about a day of the year, for example: Given an arbitrary date, how can these numbers be computed with the draft C++20 chrono specification? 回答1: This is remarkably easy with the draft C++20 chrono specification. Below I show a function which inputs an arbitrary date, and prints this information to cout . Though at the time of this writing, the draft C++20 chrono specification isn't yet shipping, it is approximated by a free,

Using (draft) C++20 chrono, how to compute various facts about a date

随声附和 提交于 2020-01-22 06:58:29
问题 https://www.timeanddate.com/date/weekday.html computes various facts about a day of the year, for example: Given an arbitrary date, how can these numbers be computed with the draft C++20 chrono specification? 回答1: This is remarkably easy with the draft C++20 chrono specification. Below I show a function which inputs an arbitrary date, and prints this information to cout . Though at the time of this writing, the draft C++20 chrono specification isn't yet shipping, it is approximated by a free,

How is the three-way comparison operator different from subtraction?

こ雲淡風輕ζ 提交于 2020-01-22 04:56:33
问题 There's a new comparison operator <=> in C++20. However I think in most cases a simple subtraction works well: int my_strcmp(const char *a, const char *b) { while (*a == *b && *a != 0 && *b != 0) { a++, b++; } // Version 1 return *a - *b; // Version 2 return *a <=> *b; // Version 3 return ((*a > *b) - (*a < *b)); } They have the same effect. I can't really understand the difference. 回答1: The operator solves the problem with numeric overflow that you get with subtraction: if you subtract a

Modern C++ way to repeat code for set number of times

十年热恋 提交于 2020-01-15 03:24:11
问题 Very simply, is there a simpler way to repeat a block for a certain number of times, where the block inside does not need the counter variable? The trivial solution is of course for (int i = 0; i < repetitions; ++i) { //do your thing, i is not used here } However, now that we have ranged for, standard algorithms and other fancy constructs for iterating over containers, in comparison this is actually starting to feel like a lot of boilerplate and details for what should be an even simpler case

Use 'using enum' in C++20 in classes possible?

怎甘沉沦 提交于 2020-01-14 18:56:10
问题 In this answer it was mentioned that in the upcoming C++20 standard it is possible to use the using statement on enum class and import the enum fields into the local namespace. I was wondering if that also means that I can also use it within class definitions like this: class Foo { enum class Color { red, blue }; using enum Color; }; int main() { Foo::Color c = Foo::red; } Or do I still need to give the full namespace?: Foo::Color c = Foo::Color::red; I tried it in wandbox.org, but it seems

Use 'using enum' in C++20 in classes possible?

浪子不回头ぞ 提交于 2020-01-14 18:54:24
问题 In this answer it was mentioned that in the upcoming C++20 standard it is possible to use the using statement on enum class and import the enum fields into the local namespace. I was wondering if that also means that I can also use it within class definitions like this: class Foo { enum class Color { red, blue }; using enum Color; }; int main() { Foo::Color c = Foo::red; } Or do I still need to give the full namespace?: Foo::Color c = Foo::Color::red; I tried it in wandbox.org, but it seems

Constexpr member function

大城市里の小女人 提交于 2020-01-14 10:28:08
问题 Suppose I have a struct template S that is parametrized by an engine: template<class Engine> struct S; I have two engines: a "static" one with a constexpr member function size() , and a "dynamic" one with a non- constexpr member function size() : struct Static_engine { static constexpr std::size_t size() { return 11; } }; struct Dynamic_engine { std::size_t size() const { return size_; } std::size_t size_ = 22; }; I want to define size() member function in S that can be used as a constexpr if