rvalue-reference

std::thread and rvalue reference

不问归期 提交于 2019-12-11 12:52:36
问题 I wanted to have some kind of delegator class. Shortened version of my approach is below and it's main functionality is to start new thread doing some thing (in this example it prints text every second): void Flusher::start(){ m_continue.store(true); m_thread = std::thread([](std::atomic<bool>& shouldContinue){ while(shouldContinue.load()){ std::this_thread::sleep_for(std::chrono::seconds(1)); std::cout << "sec passed" << std::endl; }}, std::ref<std::atomic<bool>>(m_continue) ); } My concern

Rvalue reference to lvalue reference

风格不统一 提交于 2019-12-11 11:26:21
问题 Rvalues cannot be used to initialize lvalue (normal) references. But if I write a helper conversion function, it works. What is going on in the background and is it possibly dangerous? template <class T> inline T& getLvalueRef(T&& x) { return x; } The compiler accepts this. And then int ten = 10; int& ref = getLvalueRef(ten+8); // use some variables to rewrite memory int a = 7; int b = 10; int c = (a+b)*6; // check if still works cout << ref << endl; // okay, still 18 ref = 9; cout << ref <<

Which versions of gdb and gcc allow watching rvalue references?

十年热恋 提交于 2019-12-11 11:05:54
问题 I am using GDB 7.8.0.20140729-cvs and GCC 4.8.2 . Whenever I try to print the value of a variable referenced by an rvalue reference, I get an error from the debugger complaining about an unknown type, forcing me to manually cast the T && to a T * . Are there newer versions of these where this bug is fixed? Ideally I'd prefer not to upgrade GCC if I don't have to? 回答1: Are there newer versions of these where this bug is fixed? No. Upgrading GCC won't help because it is already doing the right

When a function takes an rvalue reference, what is the type of that variable within the function?

烂漫一生 提交于 2019-12-11 06:05:12
问题 This is a question of terminology. If I have this: #include <vector> void g(std::vector<int>&& arg); void f0(std::vector<int>&& v) { static_assert(std::is_same<decltype(v), std::vector<int>&&>::value); // Looks like v is an rvalue reference. static_assert(std::is_same<decltype((v)), std::vector<int>&>::value); static_assert(std::is_same<std::decay<decltype(v)>::type, std::vector<int>>::value); return g(std::move(v)); // Fine. } then what type is v ? If you are talking about calling f0 , you'd

Is it a good practice to return the r-value reference from the r-value ref-qualified method?

这一生的挚爱 提交于 2019-12-11 03:02:50
问题 As I can see the general rule is not to return r-value references from functions at all (except for rare special cases). But what about class methods? There is an example in the C++ standard library of returning r-value reference from the r-value ref-qualified method of the class (std::optional<T>::operator*() and std::optional<T>::value() methods of the std::optional<T> class). See sections 23.6.3 Class template optional [optional.optional] and 23.6.3.5 Observers [optional.observe] of the C+

Preferred parameter passing for constructors

江枫思渺然 提交于 2019-12-11 01:35:10
问题 Is there a preferred practice for passing constructor parameters? In particular if those constructor parameters are used to initialize member variables. A simplified example. class Example { public: Example( /*type-1*/ str, /*type-2*/ v ): m_str( str ), m_v( v ) { } /* other methods */ private: std::string m_str; std::complex<float> m_v; }; The options are: pass-by-value, then std::move the object into the member. const& , then copy the parameter into the member. && , then initialize the

Returning an rvalue reference from a nonlocal

旧时模样 提交于 2019-12-11 01:21:57
问题 I have a class that is queried for an internal state object: class State {...}; //Has a copy and move constructor class Processor { private: std::unique_ptr<State> state; public: void process(...) { State newState; ... //create this new state state.reset(new State(newState)); } State getState() { return std::move(*state.release()); } }; Is this an appropriate use of std::move ? I can guarantee that getState will only be called once per call to process , but because of the design of this

nested std::forward_as_tuple and segmentation fault

浪子不回头ぞ 提交于 2019-12-11 00:29:35
问题 My actual problem is a lot more complicated and it seems extremely difficult to give a short concrete example here to reproduce it. So I am posting here a different small example that may be relevant, and its discussion may help in the actual problem as well: // A: works fine (prints '2') cout << std::get <0>(std::get <1>( std::forward_as_tuple(3, std::forward_as_tuple(2, 0))) ) << endl; // B: fine in Clang, segmentation fault in GCC with -Os auto x = std::forward_as_tuple(3, std::forward_as

Does C++11's && (R-value reference) operator obsolete the 'proxy-object' design-pattern?

泄露秘密 提交于 2019-12-10 22:37:32
问题 Item 30 of Scott Meyers' "more effective C++" maps out a 'proxy object' programming design-pattern. The problem is if you have: X x; x[3]=42; cout<<x[3] ... you need X's operator[] overload to be able to distinguish between L-value and R-value use. (maybe you need different code to run,e.g. in the first case heavy copying might be involved, but in the second case maybe we can just pass back a reference). The proxy pattern is that X contains a Proxy class, and X's operator[] overloads return

Return rvalue reference vs return by value in function return type [duplicate]

南笙酒味 提交于 2019-12-10 21:42:37
问题 This question already has answers here : c++11 Return value optimization or move? [duplicate] (4 answers) Closed 4 years ago . In my code I have a function that constructs a string from a piece of data and then returns it. This string isn't used anywhere else, so it's safe for the receiving side to use move-assignment or move-initialization on it. std::string ReadString(...) { ... return std::string(...) } This is basically what I have. Is there any point in making the function return type