c++17

Why Sortable concept requires totally ordered value type, while std::sort only requires “less than” comparable?

不羁岁月 提交于 2019-12-06 18:21:58
问题 In the latest paper on concepts N3701, there is the following example with the sort algorithm: template<typename Cont> requires Sortable<Cont>() void sort(Cont& cont) where Sortable concept is defined as template<typename T> concept bool Sortable() { return Permutable_container<T>() && Totally_ordered<Value_type<T>>(); } where Totally_ordered , not surprisingly, is defined as template<typename T> constexpr bool Totally_ordered() { return Weakly_ordered<T>() && Equality_comparable<T>(); } and

Is a shared_ptr's deleter stored in memory allocated by the custom allocator?

自闭症网瘾萝莉.ら 提交于 2019-12-06 16:57:05
问题 Say I have a shared_ptr with a custom allocator and a custom deleter. I can't find anything in the standard that talks about where the deleter should be stored: it doesn't say that the custom allocator will be used for the deleter's memory, and it doesn't say that it won't be. Is this unspecified or am I just missing something? 回答1: util.smartptr.shared.const/9 in C++ 11: Effects: Constructs a shared_ptr object that owns the object p and the deleter d. The second and fourth constructors shall

Constexpr if with a non-bool condition

[亡魂溺海] 提交于 2019-12-06 16:29:54
问题 I seem to have found something that Clang and GCC disagree on. Here's the code: int main() { if constexpr (2) {} } This successfully compiles with GCC 7.4.0, but it fails with Clang 7.0.0 with this error message: test.cpp:3:17: error: constexpr if condition evaluates to 2, which cannot be narrowed to type 'bool' [-Wc++11-narrowing] if constexpr (2) {} ^ 1 error generated. cppreference doesn't seem to mention "narrowing", so this seems like a Clang bug, but I'm not entirely certain. If this is

Disallow the use of auto for a given class, C++14 vs. C++17 update

谁说我不能喝 提交于 2019-12-06 15:31:25
问题 What is the feature that allows me use auto for non-copyable (and non-movable) types in C++17 and didn't for C++14? Consider the following code: struct A{ A(A const&)=delete; A(A&&)=delete; }; int main(){ auto a1 = A{}; // ok in C++17, not ok in C++14 auto&& a2 = A{}; // ok in C++17, ok in C++14 } It turns out that this was invalid code in C++14 but it is valid in C++17. The behavior is consistent in clang and gcc: https://godbolt.org/z/af8mEc The reason I ask is because until recently I was

Copy/assignment of fundamental types

白昼怎懂夜的黑 提交于 2019-12-06 10:55:26
问题 What does the standard say about copy/assignment of fundamental types? For class types, we have copy constructor, assignment operator, which takes the right hand side as a reference (it must be a reference, otherwise we had infinite recursion): struct Foo { Foo(const Foo &); }; How does this defined for fundamental types? Look at this example: const Foo foo; Foo f = foo; const int a = 2; int b = a; Here, f = foo; odr-uses foo , as copy-constructor takes a reference, right?. If copy of

Writing a Factory method for STL random number generators

北战南征 提交于 2019-12-06 09:57:47
I'm trying to provide an interface — through a config file — for my users to choose a distribution for some of the parameters that they are using. I would like to use STL random number generator algorithms for this purpose. Let's assume that my program reads a JSON from a command line. For the JSON provided below, the program needs to realize that it should generate a random number from the normal distribution with given mean and standard variation. (I'm using the same parameter names as STL library for clearance.) { "dist": "normal_distribution", "mean": 0.1, "stddev": 0.5 } So far, I can

Why calling shared_from_this calls std::terminate

空扰寡人 提交于 2019-12-06 09:03:35
Consider this code: class A : public std::enable_shared_from_this<A> { public: std::shared_ptr<A> f() { return shared_from_this(); } }; int main() { A a; std::shared_ptr<A> ptr = a.f(); } This code terminated in Visual Studio 2017. I guess I am doing something wrong here. Can anyone help me with this? I want a shared_ptr on a created by shared_from_this(). Because a is not a owned by a shared pointer. From cppreference : It is permitted to call shared_from_this only on a previously shared object, i.e. on an object managed by std::shared_ptr. Otherwise the behavior is undefined (until C++17)std

Range/Loop through N variables in [modern] C++

不打扰是莪最后的温柔 提交于 2019-12-06 06:48:34
What's a succinct way of ranging through N variables, of any type each, to perform an operation? Let's say I have variables a , b , c , d , e and want to go through all of them performing some operation. Use Boost.Hana and generic lambdas: #include <tuple> #include <iostream> #include <boost/hana.hpp> #include <boost/hana/ext/std/tuple.hpp> struct A {}; struct B {}; struct C {}; struct D {}; struct E {}; int main() { using namespace std; using boost::hana::for_each; A a; B b; C c; D d; E e; for_each(tie(a, b, c, d, e), [](auto &x) { cout << typeid(x).name() << endl; }); } http://coliru.stacked

How to define a variant<x,y,z> extracting subtypes of a template parameter

风流意气都作罢 提交于 2019-12-06 06:42:13
问题 I am building a state-machine where state transitions are described as a variant, i.e.: using table = std::variant< /* state event followup-state */ transition<start, success<sock>, connecting>, transition<start, exception, failed>, transition<connecting, success<>, connected>, transition<connecting, exception, failed>, transition<connected, exception, failed> >; and transition being a simple type: template <typename ENTRY_STATE, typename EVENT, typename NEXT_STATE> struct transition { using

How to implement std::when_any without polling?

岁酱吖の 提交于 2019-12-06 05:35:52
Consider http://en.cppreference.com/w/cpp/experimental/when_any . The following is just a naive and simplified implementation: #include <future> template<typename Iterator> auto when_any(Iterator first, Iterator last) { while (true) { for (auto pos = first; pos != last; ++pos) { if (pos->is_ready()) { return std::move(*pos); } } } } I am not satisfied because it is a busy polling in an infinite loop. Is there a way to avoid busy polling? A polling free version would launch 1 thread per future and have them set a condition variable with which future is ready. Then you "leak" the threads until