rvalue-reference Questions

1

#include <iostream> using namespace std; void func(int (&ref)[6]) { cout << "#1" << endl; } void func(int * &&ref) { cout << "#2" << endl; } int main() ...
Reich asked 8/9, 2017 at 12:6

1

Solved

Say I have the following function void doWork(Widget && param) // param is an LVALUE of RRef type { Widget store = std::move(param); } Why do I need to cast param back to an rval...

4

Solved

Why won't the compiler automatically deduce that a variable is about to go out of scope, and therefore let it be considered an rvalue-reference? Take for example this code: #include <string&gt...
Sidestroke asked 18/8, 2017 at 19:38

1

Solved

I do not understand why the following code compiles on GCC 8.0: decltype(auto) foo(int&& r) { return r; } In foo, the declaration type of r is int&&, and so the return type of f...
Claver asked 6/8, 2017 at 12:26

3

Solved

I've been testing some C++11 features from some some. I came across r-value references and move constructors. I implemented my first move constructor, here it is: #include <iostream> #inclu...
Tarnopol asked 6/8, 2013 at 16:17

4

Solved

I would like to implement a function drop_if. Given a unary predicate and a sequential container, it returns a container of the same type holding only the elements from the original one not fulfill...
Outlawry asked 14/6, 2017 at 10:43

1

Solved

Why can't we implement both methods getAB() && and getAB(), but can implement any one of these? Works: http://ideone.com/4EgObJ Code: struct Beta { Beta_ab ab; Beta_ab &&am...
Irresolute asked 10/6, 2017 at 21:36

2

Solved

I have an untemplated functor object that I'm trying to store as a std::function inside another object. This object is really heavyweight, so it's marked as uncopyable, but it does have a move cons...
Narcolepsy asked 19/5, 2013 at 20:24

2

Solved

I've been having difficulties understanding move constructors in C++. I have made a simple class with a default constructor, copy constructor, move constructor and destructor. Also, I have defined ...

2

Solved

So I understand that s2 binds to the expression s1 + s1, but is this evaluated at the time s2 is assigned or is it lazy and evaluated when s2 += "Test"; is called? And also would s2 hold memory for...
Stromboli asked 1/6, 2017 at 4:15

1

Solved

std::optional::value() has the following two overloads constexpr T& value() &; constexpr const T & value() const &; constexpr T&& value() &&; constexpr const T&amp...
Insinuation asked 31/5, 2017 at 1:6

1

Solved

I want to have a method that can be called only with lvalues so I've done the following: template <typename T> MyClass& cache(T&) {} template <typename T> MyClass& cache(c...
Zigzag asked 1/5, 2017 at 12:12

1

Solved

I have some strange behavior on std::vector with adding rvalues: #include <iostream> #include <vector> class A { public: A():i_{5}{std::cout << "default" << std::en...
Varlet asked 5/4, 2017 at 15:2

4

Solved

With rvalue references, many redundant copies may be elided, but that seems to require me to write the same function multiple times (one for an rvalue reference, one for a const lvalue reference). ...
Uriel asked 23/3, 2017 at 13:36

2

Solved

I'm trying to write a function that returns a subset of a variadic argument pack under the form of an std::tuple. The function should ideally have no runtime overhead (no unnecessary copies), and i...
Secretin asked 29/10, 2015 at 17:52

1

Solved

#include <vector> using namespace std; struct A { vector<int> coll; }; void f(const vector<int>&){} void f(vector<int>&&){} int main() { f(A().coll); // Is...
Mattison asked 24/2, 2017 at 16:59

1

Solved

#include <vector> using namespace std; struct A { A(const vector<int>&) {} A(vector<int>&&) {} }; A f() { vector<int> coll; return A{ coll }; // Which con...
Wieren asked 24/2, 2017 at 16:22

1

Solved

void f(int & i){ cout << "l-value-ref" << endl; } void f(int && i){ cout << "r-value-ref" << endl; } Assuming the code above, we have a overloaded function ...
Sunnisunnite asked 22/2, 2017 at 12:0

3

Solved

I'm relativly new to C++ and this might be a foolish question but I'm trying to understand rValue and lValue references at the moment and this came to my mind: Let's say we have a map (which comes...
Hesiod asked 16/2, 2017 at 18:55

6

Solved

UPDATE at the bottom q1: How would you implement the rule of five for a class that manages rather heavy resources, but of which you want it to be passed around by value because that greatly simpli...
Simms asked 12/5, 2011 at 10:11

4

I have a class that 'remembers' a reference to some object (e.g. an integer variable). I can't have it reference a value that's destructed immediately, and I'm looking for a way to protect the user...

1

When I was reading Effective Modern C++ by Scott Meyer about how std::forward function works, I got one question that I don't quite understand. Say if we have a function foo as follows: template&l...
Schizothymia asked 19/12, 2016 at 5:40

4

Solved

So my understanding of move semantics is that they allow you to override functions for use with temporary values (rvalues) and avoid potentially expensive copies (by moving the state from an unname...

3

Solved

#include <iostream> #include <string> void fnc (const std::string&) { std::cout<<1; } void fnc (std::string&&) { std::cout<<2; } int main() { fnc ("abc"); ...
Bodice asked 12/11, 2016 at 15:57

1

Solved

I try to define a class A as follows: template< typename T > class A { public: A( T elem ) : _elem( elem ) {} private: TYPE _elem; // "TYPE" should be either "T" in case "elem" is an ...
Christianize asked 11/11, 2016 at 10:28

© 2022 - 2024 — McMap. All rights reserved.