c++20

How to use template explicit instantiation with C++20 modules?

◇◆丶佛笑我妖孽 提交于 2020-05-13 12:07:51
问题 As explained in this answer template instantiation allows reducing compilation times and sizes by not requiring templates to be recompiled for every new type in every new file that uses them. I'm also excited about how C++20 modules should provide a clean solution to expose templates to external projects and reduce hpp/cpp duplication. What is the syntax that will allow them to work together? For example, I expect modules to look a bit like (untested and therefore likely wrong code because I

How is the best constrained function template selected with concepts?

≡放荡痞女 提交于 2020-05-13 09:45:40
问题 In a presentation of concepts something like this was shown: template <bidirectional_iterator It> void sort(It begin, It end); // #1 template <random_access_iterator It> void sort(It begin, It end); // #2 std::list<int> l{}; sort(l.begin(), l.end()); // #A -> calls #1 std::vector<int> v{}; sort(v.begin(), v.end()); // #B -> calls #2 For the call #A it's simple: only sort #1 is viable as the constraint random_access_iterator is not satisfied so it calls #1 . But for the call #B both sort s are

How is the best constrained function template selected with concepts?

蓝咒 提交于 2020-05-13 09:42:07
问题 In a presentation of concepts something like this was shown: template <bidirectional_iterator It> void sort(It begin, It end); // #1 template <random_access_iterator It> void sort(It begin, It end); // #2 std::list<int> l{}; sort(l.begin(), l.end()); // #A -> calls #1 std::vector<int> v{}; sort(v.begin(), v.end()); // #B -> calls #2 For the call #A it's simple: only sort #1 is viable as the constraint random_access_iterator is not satisfied so it calls #1 . But for the call #B both sort s are

C++ concepts compound requirements and return-type-requirements

守給你的承諾、 提交于 2020-05-11 07:18:43
问题 The last time I used C++ concepts with GCC and the fconcepts flag the following snippet used to work template <typename T, typename U> concept equality_comparable = requires(T a, U b) { { a == b } -> bool; { a != b } -> bool; }; Apparently this is no longer the case and a return-type-requirement after a compound requirement can now only contain type constraints. If I'm not mistaken this basically means using another concept to satisfy the return-type-requirement . So the perfectly readable

Why can't concept refinement use the terse syntax

瘦欲@ 提交于 2020-04-29 10:38:19
问题 When refining concepts, the way it is consistently done in the standard is to fully write out the concept being refined. For instance, in [concepts.integral], SignedIntegral refines Integral like so: template<class T> concept Integral = is_integral_v<T>; template<class T> concept SignedIntegral = Integral<T> && is_signed_v<T>; Why can't the refined concept be written as: template<Integral T> concept SignedIntegral2 = is_signed_v<T>; SignedIntegral2 seems to have the same obvious meaning of

Why is std::move not [[nodiscard]] in C++20?

早过忘川 提交于 2020-04-29 08:47:10
问题 I've recently read about [[nodiscard]] in C++17, and as far as I understand it's a new feature (design by contract?) which forces you to use the return value. This makes sense for controversial functions like std::launder (nodiscard since C++20), but I wonder why std::move isn't defined like so in C++17/20. Do you know a good reason or is it because C++20 isn't finalised yet? 回答1: AFAIK P0600R1 is the only proposal for adding [[nodiscard]] to the standard library that was applied to C++20.

Equality operator does not get defined for a custom spaceship operator implementation in C++20

一个人想着一个人 提交于 2020-04-29 07:51:20
问题 I'm running into a strange behavior with the new spaceship operator <=> in C++20. I'm using Visual Studio 2019 compiler with /std:c++latest . This code compiles fine, as expected: #include <compare> struct X { int Dummy = 0; auto operator<=>(const X&) const = default; // Default implementation }; int main() { X a, b; a == b; // OK! return 0; } However, if I change X to this: struct X { int Dummy = 0; auto operator<=>(const X& other) const { return Dummy <=> other.Dummy; } }; I get the

What is the correct syntax for concept in combination with a templated struct or class?

主宰稳场 提交于 2020-04-16 06:08:09
问题 Recently I was using concept s to define different constructors for a template d struct . Here is the code: #include <iostream> namespace detail { template<typename T, typename U > concept SameHelper = std::is_same_v<T, U>; } template<typename T, typename U > concept same_as = detail::SameHelper<T, U> && detail::SameHelper<U, T>; template<typename T> concept trivial = same_as<T, bool> || same_as<T, char>; template<typename T> concept not_trivial = !trivial<T>; template<typename T> struct Foo

Alternating Template Parameters Pack

孤人 提交于 2020-04-10 04:58:08
问题 How do I achieve a pack of alternating template parameters? Something like this: template< ( unsigned non_type, typename type )... > Where the usage must be my_class< 5U, float, 6U, std::string > I don't want to change the order nor do I want to have a wrapper around it as a pair during usage. Obviously, if it devolves into some sort of a pair in my own internal implementation, that's fine. 回答1: The thing to remember about templates is that they're not macros . They are not copying tokens

Alternating Template Parameters Pack

ε祈祈猫儿з 提交于 2020-04-10 04:55:26
问题 How do I achieve a pack of alternating template parameters? Something like this: template< ( unsigned non_type, typename type )... > Where the usage must be my_class< 5U, float, 6U, std::string > I don't want to change the order nor do I want to have a wrapper around it as a pair during usage. Obviously, if it devolves into some sort of a pair in my own internal implementation, that's fine. 回答1: The thing to remember about templates is that they're not macros . They are not copying tokens