Questions tagged [rvalue-reference]
An rvalue reference is a new language feature in C++11 representing a reference to an rvalue. Together with reference collapsing, they are used to implement and enable move semantics and perfect forwarding.
rvalue-reference
1,109
questions
2
votes
0
answers
71
views
Returning a moved parameter on failure
Is it an acceptable pattern to return a moved function parameter in case some kind of problem occurred? For example when trying to store data in a database, but validation failed and the data handle ...
5
votes
1
answer
169
views
Modern ways to implement assignment for value types
This question is about how to implement assignment in modern C++ value types, with the goal of avoiding misleading code or code that is inconsistent with built-in behavior.
In C++, generated values (...
2
votes
2
answers
97
views
Conditional use of std::move
I'm trying to forward an r-value reference from a template class Foo to another template class Bar as follows:
#include <iostream>
#include <string>
#include <type_traits>
#include &...
1
vote
1
answer
96
views
C++ Does Ranged-Based For Loop Use RValue Reference? [duplicate]
Hi I have a quick question - over here it says ranged-based for loops of the form
for ( init-statement (optional) range-declaration : range-expression )
are equivalent to the code:
{
auto &&...
1
vote
1
answer
44
views
Boost:any rvalue vs lvalue overload resolution
I want to have function overloading, with the functions accepting boost::any lvalue and boost::any rvalue as arguments and exhibiting different behaviour based on that. A minimum reproducible example;
...
3
votes
1
answer
83
views
std::set<std::unique_ptr<int>>and std::views::as_rvalue
I have the following code where for some reason only last block does not work.
I originally thought it may be related to unique_ptr being move only or std::set having const keys, but then it is ...
1
vote
3
answers
100
views
Lifetime of rvalue of custom class bound by rvalue reference
#include <iostream>
using namespace std;
class Someclass
{
public:
~Someclass()
{
cout << "Someclass Dtor called" << endl;
}
};
int main()
{
...
2
votes
2
answers
73
views
lvalue and rvalue references
I am having a hard time understanding lvalue and rvalue references. Look at this code example
#include <iostream>
//Returns r-value
int Add(int x, int y) {
return x + y;
}
//Return l-value
...
3
votes
1
answer
49
views
How does std::pair constructor overload taking tuples tell if tuple contents are r-val references?
Apologies for the awkwardly worded question. My uncertainty is based on a situation that can roughly be reduced to the following scenaio.
I have a Foo class, and I'd like a std::pair<int, Foo> ...
1
vote
1
answer
76
views
Are rvalue references Cpp17MoveConstructible?
Do rvalue references satisfy the Cpp17MoveConstructible requirement?
For this to be the case, in the expression T u = rv;, u would need to be "equivalent" to rv. If T was an rvalue reference ...
1
vote
2
answers
121
views
C++ difference in forwarding references between template arguments and lambda's `auto` parameters
Compare the following two situations:
template<class... Args>
void f1(Args&&... args) { do_smth(std::forward<Args>(args)...); }
auto f2 = [](auto&&... args) { do_smth(std::...
0
votes
1
answer
86
views
Why const reference is called and not rvalue reference and how std::move works? [duplicate]
#include <iostream>
void bar(std::string& s) { std::cout << "ref" << std::endl;}
void bar(const std::string& s) { std::cout << "const ref" << ...
0
votes
0
answers
114
views
How to pass derived object as rvalue reference into base class pointer members using move constructor?
I implemented composite design pattern for calculating the nested mathematical expressions modeled in a binary tree. I need to hold two base class pointers *left,*right in my derived composite class. ...
-2
votes
1
answer
63
views
How can an object find that is rvalue reference(unnamed value) in C++? [duplicate]
I have a class named Matrix. I want overload operator ! that return transpose of matrix.
When the main matrix is a unnamed object I prefer to use it's allocated memory to construct transpose matrix, ...
2
votes
1
answer
148
views
Is it possible to write a class that can store lvalue references or objects exlusively?
I'm sick of passing all my objects by value and then moving, or overloading on lvalues and rvalues. I'm trying to write a class that can either store an lvalue reference or a normal object if an ...