c++17

Why is S::x not odr-used?

谁说我不能喝 提交于 2019-11-29 22:05:11
Consider this example from cppreference : struct S { static const int x = 1; }; void f() { &S::x; } // discarded-value expression does not odr-use S::x I agree that &S::x is a discarded-value expression , since the standard says (9.2, paragraph 1 [stmt.expr] from n4700 ) Expression statements have the form expression-statement: expression_opt ; The expression is a discarded-value expression (Clause 8)... However, is that enough for S::x to not be odr-used ? 6.2, paragraph 3 [basic.def.odr] states A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless

What's the difference between static constexpr and static inline variables in C++17?

匆匆过客 提交于 2019-11-29 21:15:45
With C++17 we get inline variables. One of use for them is to define constant fields in classes. So what's the difference between these two constant definitions: class MyClass { static constexpr int myFirstVar = 10; static const inline int mySecondVar = 100; }; Of course constexpr makes myFirstVar implicitly inline. What's the better choice here, to use constexpr or inline ? Note: when you don't need constness, then inline makes it easier. With constexpr you don't have that choice. You don't have to specify an initializer for mySecondVar at the point of declaration. Nor is the initializer

How does std::visit work with std::variant?

假装没事ソ 提交于 2019-11-29 18:16:24
问题 I'm looking at std:variant/std::visit doc here: http://en.cppreference.com/w/cpp/utility/variant/visit and also googled a lot trying to understand the magic behind std::visit and std::variant . So my question is the following. In the provided example, both in the polymorphic lambda and the "overloaded" there is some "magic" happening that makes it possible to extract the correct type from std::variant . So looking at this: for (auto& v: vec) { std::visit(overloaded { [](auto arg) { std::cout

Can I initialize an array using the std::initializer_list instead of brace-enclosed initializer?

心不动则不痛 提交于 2019-11-29 17:37:03
问题 Can I initialize an array using the std::initializer_list object instead of brace-enclosed initializer? As known, we can do this: http://en.cppreference.com/w/cpp/language/aggregate_initialization unsigned char b[5]{"abc"}; // equivalent to unsigned char b[5] = {'a', 'b', 'c', '\0', '\0'}; int ar[] = {1,2,3}; std::array<int, 3> std_ar2{ {1,2,3} }; // std::array is an aggregate std::array<int, 3> std_ar1 = {1, 2, 3}; But I can't initialize an array by std::initializer_list il; : http://ideone

Does C++ standard library provide more compact and generalized version of the erase–remove idiom?

你说的曾经没有我的故事 提交于 2019-11-29 17:16:25
问题 We can erase one element/ entry from a container by the popular erase–remove idiom. However, many of us would have encountered some problems while applying this idiom: one can easily get into the pitfall of typos like c.erase(std::remove_if(c.begin(), c.end(), pred)); // , c.end() //---> missing here or c.erase((std::remove_if(c.begin(), c.end(), pred), c.end())) // ^^ ^^ // extra () makes it pass only c.end() to the c.erase It even follows the wrong semantics for containers like std::list by

What is run first inside a cout statement? (C++17)

自古美人都是妖i 提交于 2019-11-29 16:44:38
Say for example I have a long statement like cout << findCurrent() << "," << findLowest() << "," << findHighest() << "," << findThird()<<"\n"; would findCurrent() be run before findLowest() like logic dictates? Since C++17 the functions are guaranteed to be called left-to-right, i.e. findCurrent() is called first, then findLowest() and so on. C++17 Standard references: [expr.shift]/4 (referring to the expression E1 << E2 ): The expression E1 is sequenced before the expression E2 . [over.match.oper]/2: (describing overloaded operators) the operands are sequenced in the order prescribed for the

May I change the held type in a std::variant from within a call to std::visit

我怕爱的太早我们不能终老 提交于 2019-11-29 16:24:56
问题 Does the following code invoke undefined behaviour? std::variant<A,B> v = ...; std::visit([&v](auto& e){ if constexpr (std::is_same_v<std::remove_reference_t<decltype(e)>,A>) e.some_modifying_operation_on_A(); else { int i = e.some_accessor_of_B(); v = some_function_returning_A(i); } }, v); In particular, when the variant does not contain an A , this code re-assigns an A while still holding a reference to the previously held object of type B . However, because the reference is not used

Difference between execution policies and when to use them

若如初见. 提交于 2019-11-29 16:23:29
问题 I noticed that a majority (if not all) functions in <algorithm> are getting one or more extra overloads. All of these extra overloads add a specific new parameter, for example, std::for_each goes from: template< class InputIt, class UnaryFunction > UnaryFunction for_each( InputIt first, InputIt last, UnaryFunction f ); to: template< class ExecutionPolicy, class InputIt, class UnaryFunction2 > void for_each( ExecutionPolicy&& policy, InputIt first, InputIt last, UnaryFunction2 f ); What effect

Can I generate a function without providing arguments?

廉价感情. 提交于 2019-11-29 16:23:10
So c++17 has std::function Deduction Guides so given: int foo(); I can do: std::function bar(foo); But I'm stuck on a c++14 compiler. There I have to do something more like: function<int()> bar(foo) . I was wondering if there was a way to create a std::function without passing the function pointer and explicitly providing the function signature? So for example make_pair will deduce the type of it's return from it's arguments. I was wondering if I could write something similar for function s even using c++14 , like: auto bar = make_function(foo); Is this doable? Note: My real case is that foo

std::shared_mutex with std::shared_lock is reader or writer preferring?

强颜欢笑 提交于 2019-11-29 14:58:13
In implementation of reader-writer lock, we can make use of the std::shared_mutex with std::shared_lock and std::lock_guard or std::unique_lock . Question > Is this new feature writer or reader preferring? Update based on Andrew's comment Reference : // Multiple threads/readers can read the counter's value at the same time. unsigned int get() const { std::shared_lock<std::shared_mutex> lock(mutex_); return value_; } // Only one thread/writer can increment/write the counter's value. void increment() { std::unique_lock<std::shared_mutex> lock(mutex_); value_++; } As you can see from above