c++14

Is there a `shared_lock_guard` and if not, what would it look like?

故事扮演 提交于 2019-12-29 01:43:11
问题 I wanted to use a std::mutex in my class, and noticed that it isn't copyable. I'm at the bottom level of my library here, so it seems like a terrible idea to have this behaviour. I used std::lock_guard on the std::mutex , but there doesn't seem to be a shared_lock_guard , which would be preferable to provide write-locks-exclusively behaviour. Is this an oversight or trivial to implement myself? 回答1: With C++14 You can use a std::shared_lock and a std::unique_lock to implement read/write

ADL using std types fails to find operator

*爱你&永不变心* 提交于 2019-12-28 22:17:06
问题 The following code fails to compile namespace A { using C = std::vector<std::string>; std::ostream& operator << (std::ostream& lhs, const C& rhs) { lhs << 5; return lhs; } } int main() { A::C f; std::cout << f; return 0; } with the error Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'A::C' (or there is no acceptable conversion) Obviously it cant find the << operator presumably due to considering C to be a class from the std namespace. Is there some way to

Understanding Y Combinator through generic lambdas

不羁岁月 提交于 2019-12-28 16:09:42
问题 While building a small lambda-based metaprogramming library, I had the necessity of using recursion in a C++14 generic lambda, to implement a left-fold. My own solution was passing the lambda itself as one of its parameters, like this: template <typename TAcc, typename TF, typename... Ts> constexpr auto fold_l_impl(TAcc acc, TF f, Ts... xs) { // Folding step. auto step([=](auto self) { return [=](auto y_acc, auto y_x, auto... y_xs) { // Compute next folding step. auto next(f(y_acc, y_x)); //

c++11: How to write a wrapper function to make `std::function` objects

别来无恙 提交于 2019-12-28 12:34:55
问题 I am trying to write a wrapper make_function , which like std::make_pair can create a std::function object out of suitable callable objects. Just like make_pair , for a function pointer foo , auto f0 = make_function(foo); creates a std::function function object f0 of the right type signature. Just to clarify, I don't mind occasionally giving type parameters to make_function in case it is difficult (or impossible) to deduce the type entirely from the parameters. What I came up with so far

c++11: How to write a wrapper function to make `std::function` objects

江枫思渺然 提交于 2019-12-28 12:34:36
问题 I am trying to write a wrapper make_function , which like std::make_pair can create a std::function object out of suitable callable objects. Just like make_pair , for a function pointer foo , auto f0 = make_function(foo); creates a std::function function object f0 of the right type signature. Just to clarify, I don't mind occasionally giving type parameters to make_function in case it is difficult (or impossible) to deduce the type entirely from the parameters. What I came up with so far

Why is there no std::allocate_unique function in C++14?

雨燕双飞 提交于 2019-12-28 11:46:09
问题 Why does shared_ptr have allocate_shared while unique_ptr does not have allocate_unique? I would like to make a unique_ptr using my own allocator: do I have to allocate the buffer myself and then assign it to a unique_ptr? This seems like an obvious idiom. 回答1: Why does shared_ptr have allocate_shared while unique_ptr does not have allocate_unique ? shared_ptr needs it so that it can allocate its internal shared state (the reference count and deleter), as well as the shared object, using the

Variable template at class scope

余生颓废 提交于 2019-12-28 07:04:26
问题 Using N3651 as a basis, A variable template at class scope is a static data member template . The example given is: struct matrix_constants { template <typename T> using pauli = hermitian_matrix<T, 2>; Yet all of the following definitions give an error: struct foo { template <typename T> T pi = T{3.14}; }; template <typename T> struct foo2 { template <typename U = T> U pi = U{3.14}; }; template <typename T> struct foo3 { template <T> T pi = 42; }; error: member 'pi' declared as a template

Variable template at class scope

霸气de小男生 提交于 2019-12-28 07:04:18
问题 Using N3651 as a basis, A variable template at class scope is a static data member template . The example given is: struct matrix_constants { template <typename T> using pauli = hermitian_matrix<T, 2>; Yet all of the following definitions give an error: struct foo { template <typename T> T pi = T{3.14}; }; template <typename T> struct foo2 { template <typename U = T> U pi = U{3.14}; }; template <typename T> struct foo3 { template <T> T pi = 42; }; error: member 'pi' declared as a template

Try to understand compiler error message: default member initializer required before the end of its enclosing class

末鹿安然 提交于 2019-12-28 04:23:39
问题 I try next code with three compilers (msvc2017, gcc8.2, clang7.0) and msvc2017 works all the way, but gcc and clang not. I want to understand what is wrong with my code, and why compiler can't compile it. #include <cassert> #include <iostream> #include <cstdlib> class Downloader { public: struct Hints { int32_t numOfMaxEasyHandles = 8; //Hints(){} // <= if I uncomment this all works gcc+clang+msvc //Hints() = default; // <= if I uncomment this neither clang no gcc works (msvc - works) };

Can returning a local variable by value in C++11/14 result in the return value being constructed by rvalue when no copy/move is involved?

戏子无情 提交于 2019-12-27 12:25:47
问题 I know that in the following situation that the compiler is free to move-construct the return value from makeA (but is also free to elide the copy or move altogether): struct A { A(A&); A(A&&); }; A makeA() { A localA; return localA; } What I wonder is whether the compiler is allowed to construct an object of type A from a local object of type B by rvalue reference if it is being constructed in the return statement. In other words, in the following example, is the compiler allowed to select A