c++14

Can a shared lock on a std::shared_timed_mutex be upgraded to an exclusive lock?

别等时光非礼了梦想. 提交于 2020-01-22 10:38:30
问题 The new std::shared_timed_mutex allows for two types of locks: shared and exclusive. If one holds a shared lock, is there any way to atomically exchange it ("upgrade it") to an exclusive lock? In other words, given the following code, how can I avoid the non-atomic drop and re-lock? std::shared_timed_mutex m; //Guards a std::vector. m.lock_shared(); //Read from vector. (Shared lock is sufficient.) // ... //Now we want to write to the vector. We need an exclusive lock. m.unlock_shared(); // <-

Can a shared lock on a std::shared_timed_mutex be upgraded to an exclusive lock?

孤人 提交于 2020-01-22 10:38:09
问题 The new std::shared_timed_mutex allows for two types of locks: shared and exclusive. If one holds a shared lock, is there any way to atomically exchange it ("upgrade it") to an exclusive lock? In other words, given the following code, how can I avoid the non-atomic drop and re-lock? std::shared_timed_mutex m; //Guards a std::vector. m.lock_shared(); //Read from vector. (Shared lock is sufficient.) // ... //Now we want to write to the vector. We need an exclusive lock. m.unlock_shared(); // <-

Accessing lambda capture initialized variable outside the lambda in C++

寵の児 提交于 2020-01-21 11:51:06
问题 In C++14/17, how do you access a lambda capture initialized variable outside the scope of the lambda? Source: #include <iostream> using namespace std; int main(){ auto test = [value1 =0]() mutable {value1+=1; return value1;}; cout << test() << endl; cout << test() << endl; //cout << value1 << endl;//error: ‘value1’ was not declared in this scope } Output: 1 2 Is the value1 variable accessible outside the scope of the test() lambda? What is the lifetime of a lambda capture initialized variable

Return type deduction in recursive function

送分小仙女□ 提交于 2020-01-21 04:36:07
问题 Following code compiles : auto foo(int i) { if( i == 1 ) return i; else return foo(i-1)+i ; } While following doesn't, c++1y auto foo(int i) { return (i == 1) ? i : foo(i-1)+i ; } Why can't compiler deduce the return type in second case ? Am I missing something over here ? I know there's a sequence point after (i == 1) in second scenario, but that shouldn't be affecting compilation, right ? 回答1: The first works because of this rule, 7.1.6.4/11 of the latest draft Once a return statement has

Return type deduction for in-class friend functions

大城市里の小女人 提交于 2020-01-21 02:36:10
问题 Here is a little experiment with return type deduction for in-class friend functions (using Clang 3.4 SVN and g++ 4.8.1 with std=c++1y in both cases) that is not documented in the linked working paper #include <iostream> struct A { int a_; friend auto operator==(A const& L, A const& R) { return L.a_ == R.a_; // a_ is of type int, so should return bool } }; template<class T> struct B { int b_; friend auto operator==(B const& L, B const& R) { return L.b_ == R.b_; // b_ is of type int, so should

Is (or will be) the use of familiar template syntax in lambda expressions allowed?

可紊 提交于 2020-01-20 04:32:47
问题 C++14 introduced generic lambdas. While skimming through the related proposals I found N3418 by Faisal Vali, Herb Sutter and Dave Abrahams . Therein section 2.2 is titled : 2.2 Allow the use of familiar template syntax in lambda expressions and the following code examples include snippets like this one []<int N>(int (&a)[N]) {} Since such things fail to compile (with gcc, clang and Visual Studio), some questions come up : Is this an implementation issue ? What stopped this part from being

How to implement a variadic tuple_map operation?

此生再无相见时 提交于 2020-01-17 13:09:24
问题 I want to implement a function which maps a variadic list of tuples to another tuple, given a function. It applies an N-ary function f to a list of elements taken from a list of N tuples (each with size at least M) and creates a new M-element tuple from the result of these applications. For a list of N tuples, each with M elements, the call to std::make_tuple would look like this pseudocode: std::make_tuple( f(t1_1, t2_1, t3_1, ..., tN_1), f(t1_2, t2_2, t3_2, ..., tN_2), f(t1_3, t2_3, t3_3, .

Better way to sort 2 “linked” arrays? [duplicate]

半世苍凉 提交于 2020-01-17 06:20:31
问题 This question already has answers here : How can I sort two vectors in the same way, with criteria that uses only one of the vectors? (8 answers) Closed 3 years ago . I have 2 vectors which store properties that are "linked". This is what I mean: std::vector<std::string> names; std::vector<int> ids; // Name of object with an ID of 5 auto name = names[std::find(ids.begin(), ids.end(), 5) - ids.begin()]; Each object has a name and an ID, and so the object at index n has a name of name[n] and an

using std::transform the final vector remains empty

送分小仙女□ 提交于 2020-01-16 16:58:45
问题 I am not using std::transform often, however I found it very useful and I am starting replacing some for loops with this algorithm. What is wrong here? I want to keep all the elements of the vector vec that have code > 100. I would expect to have a new std::vector with 3 elements: 133, 144 and 155. But after the algorithm the size is 0. What is wrong? TEST_CASE("testing trasf1", "[tras1]") { std::vector<Test2> vec { {1,1}, {3,3}, {11,11}, {12,12}, {133,133}, {19,19}, {21,21}, {22,22}, {23,23}

Intel Pin with C++14

こ雲淡風輕ζ 提交于 2020-01-14 13:55:49
问题 The Questions I have a few questions surrounding usage of Intel Pin with C++14 or other C++ verions. There are rarely any problems compiling code from older C++ with newer versions, but since Intel Pin is manipulates instruction level, is there any undesirable side effects that might come if I compile it with C++11 or C++14? If it's ok to compile with C++11 or C++14, how do I make a rule to enable a newer version of C++ for my tool only? How do I set GCC/G++ default C++ version to latest, if