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.
1,050
questions
1
vote
1
answer
31
views
How does std::map resolve insert() overload between const lvalue reference and rvalue reference
I am writing a template library that is supposed to mirror std::map's interface. I stumbled across a question when implementing the following insert() overloads.
iterator insert( const value_type& ...
3
votes
2
answers
60
views
Named r-value reference binding vs assignment
A named rvalue reference in an lvalue, but it is an lvalue which can only bind to an rvalue, so:
int&& x = 5; // Ok because 5 is an rvalue
int&& y = x; // Error: You cannot bind an ...
0
votes
1
answer
39
views
r-value lifetime issue (stack-use-after-scope): How to move std::initializer_list
Usage
I have two classes, which give me a counter in range based for loops (bit like a simple ranges v3 lib).
// Usage with l-values
std::initializer_list<int> li = {10, 11, 12, 13, 14};
for (...
0
votes
0
answers
56
views
Why isn't a named rvalue reference turned to const lvalue reference?
I'm wondering why, in the following example, the function func has this signature:
void func<int&>(int&)
I know that named rvalue references are treated like lvalue references. I'm ...
0
votes
0
answers
42
views
rvalue parameter does not behaving like an rvalue [duplicate]
Consider the following code:
#include <memory>
class Object
{
public:
Object(int* _i) : i(std::unique_ptr<int>(_i)) {}
Object(Object&& other) : i(std::move(other.i)) {}
...
1
vote
1
answer
38
views
Can Lvalue Reference be bounded to Rvalue Reference? What will happen during the process?
I am trying to figure out the meaning of the following snippet:
int main() {
int&& a = 2;
int& b = a; // (*)
}
I know a is an lvalue expression of type "rvalue reference ...
-1
votes
1
answer
54
views
rvalue Reference not matching [duplicate]
I have this exercise in which we try various combinations of rvalue and lvalue references using a template class, I am getting two assertion errors; if someone could guide.
#include <assert.h>
...
0
votes
0
answers
60
views
C++14 lambda/function decorator
Can someone explain me, C++ unified reference?
I have this code, which works only with lambda functions, but doesn't work with function pointer. What is the problem?
`
#include <iostream>
class ...
3
votes
0
answers
52
views
Error in reinterpret_cast from and to rvalue reference
The following code
int i;
reinterpret_cast<int &&>( std::move( i ) );
in Visual Studio 2022 results in
error C2102: '&' requires l-value
GCC an Clang do not see anything wrong with ...
1
vote
1
answer
106
views
Pass ownership of an object into method of the same object?
I came across come C++ code similar to the following (more or less minimal) example. Please consider the marked method call in the function on the bottom:
#include <memory>
static unsigned ...
0
votes
0
answers
22
views
Why const return type function returns rvalue rather than const rvalue? [duplicate]
The function func overloaded by const, I think the return value of temp should be const rvalue, but why does the func(temp()) call match the func(int &&arg) and not the other?
// g++ test.cc -...
0
votes
1
answer
62
views
Why does referencing an xvalue not extend the lifetime of the object it refers to?
The compiler has no way of knowing whether the xvalue is actually referencing a temporary. Therefore if the xvalue is a reference to some specific persistent "non-temporary" object, we would ...
0
votes
1
answer
94
views
Move semantics and overload
I think my understanding of rvalue references and move semantics has some holes in it.
As far as I've rvalue references understood now, I could implement a function f in two ways such that it profits ...
0
votes
0
answers
240
views
error: cannot bind rvalue reference of type 'some_struct<int>&&' to lvalue of type 'some_struct<int>'
I'm taking a templated rvalue-reference in the function signature which should make it an universal reference. However, lvalues are still not able to bind to this parameter. Check this out:
Demo
#...
2
votes
1
answer
87
views
Why are string literals not rvalues
Consider this code:
void test(auto& arg){}
int main(){
test("bla");
test(1);
}
The call test(1); gives an error, because 1 is an rvalue
main.cpp: In function 'int main()':
main....
0
votes
1
answer
39
views
Why do references to moved values not dangle after the lvalue that the object was moved into goes out of scope? [duplicate]
While playing around with references to lambdas I encountered a scenario where I expected the program to crash due to dangling references. In the following program, I assumed that the lambda argument ...
0
votes
0
answers
44
views
Move semantics in parameter passing
I have following test example:
#include <iostream>
#include <vector>
void foo (std::vector<int> value) {
std::cout << "value "
<< &value
...
7
votes
0
answers
58
views
why I can't bind function with rvalue args like this? [duplicate]
#include <functional>
#include <iostream>
#include <string>
#include <utility>
using namespace std;
template<typename Func, typename... Args>
void do_task(Func &func,...
6
votes
2
answers
102
views
template specialization and rvalue reference, c++ [duplicate]
I'm a bit confused about this little example:
using mytype = std::vector<std::string>;
template<typename T>
void test(T item)
{
throw std::runtime_error(typeid(item).name());
}
...
3
votes
1
answer
135
views
how to understand void (*&&)() func
I use C++ https://cppinsights.io/ to see the progress of instantiation, and there's something puzzled between Function&& and Function.
I comment the code generated by cppinsights.
template<...
0
votes
0
answers
44
views
`std::optional`: How to reduce redundant copies with rvalue reference and std::move()
In this code,
How many times would the generated string be copied for each case(case 1, case 2)?
Could anyone confirm my answers in the code body?
If so, can I tell case 2 is more efficient than case ...
0
votes
0
answers
34
views
How to reduce redundant copies with rvalue reference and std::move()
In this code,
How many times would the generated string be copied for each case(case 1 ~ case 4)?
Could anyone confirm my answers in the code body?
If so, can I tell case 4 is more efficient than case ...
0
votes
1
answer
105
views
Return type for *this in a builder-class method - lvalue vs rvalue refence
Suppose I have a "builder" class B which builds a class C, and looks somewhat like the following:
class B {
public:
// ...
B& set_foo(Foo a_foo) { foo_ = std::move(a_foo); return *...
1
vote
1
answer
56
views
Make rvalues persist as pointers in a recursive class
I have a simple linked list node class which I can use to create a list 1->2->3 like so:
struct ListNode {
int data_;
ListNode *child_;
ListNode(int data, ListNode *child) : data_{...
6
votes
4
answers
240
views
Why r-value reference to pointer to const initialized with pointer to non-const doesn't create an temporary and bind it with it?
If we want to initialize an reference with an different type, we need to make it const (const type*) so that an temporary can be generated implicit and the reference binded to with. Alternativaly, we ...
0
votes
2
answers
131
views
difference between using std::move and adding 0 to the number?
I'm curious about that is there any practical difference between using std::move to convert an l-value integer to r-value, and adding a 0 to that integer? or any other neutral arithmetic operation (...
1
vote
1
answer
169
views
reassignment/move of std::future waits for existing future to complete
The main method below launches two std::asyncs. The future f in the main method is initially used to hold the future for the first async before being reassigned to the future of the second std::async....
2
votes
2
answers
92
views
Why universal reference as an input parameter doesn't work
template<typename T>
constexpr auto log_value(T&& value) {
if constexpr (std::is_enum_v<T>) {
cout << "enum" << endl;
}
else {
...
0
votes
0
answers
57
views
Why rvalue reference variables are used in std::make_pair()? [duplicate]
I was reading about the make_pair() template function in C++, and couldn't understand why its parameters are rvalue reference variables. Why shouldn't they accept lvalues as well, like a normal ...
1
vote
2
answers
137
views
Is it ok to return address of rvalue in inline functions?
I know that inline functions get copied into the code each time they are referenced. From my current understanding, I think it would be ok to return the address of an rvalue from an inline function ...
0
votes
1
answer
157
views
Is it legal to rebind an rvalue reference variable? [duplicate]
When used in a single scope, why does this compile:
float x = 3.14;
auto &&expr = 2*x + x*x; // expr binds to the result of an expression
expr = x;
Demo
First and foremost this looks like a ...
4
votes
1
answer
86
views
Interpretation of access decoration of member functions [duplicate]
In C++11 and later, one can decorate the member function with &, const&, oe && (or other combinations).
If one has several overloads, and at least one is specified like this, the ...
0
votes
1
answer
385
views
A lvalue reference can bind to a rvalue reference but cannot bind to the result of std::move [duplicate]
I have a function that has a lvalue reference as argument, and I found that I can also pass
a rvalue reference to it. However, when I passing the result of std::move to the function, I get an error ...
0
votes
2
answers
61
views
Is returned object form a fuction how's return type is rvalue reference, a rvalue or a lvalue?
I am new to c++. For this code
template <typename T>
constexpr bool is_lvalue(T&& x) {
return std::is_lvalue_reference<T>{};
}
int x=9;
int & fun(){
return x;
}
...
0
votes
1
answer
57
views
How Rvalue references bind to a temporary value (rvalue) behind the hood
I am interested in how the following code:
int&& c = 2;
c++;
std::cout << c; //3
keeps the variable 'c' in memory?
How the compiler implements the reference at the machine level? Does ...
0
votes
1
answer
59
views
C++17: non-const reference to temporary from method returning *this
I'm sure this has been asked before, but my googling skills weren't sufficient to find my case. Simply put, is the following valid C++ code?
struct A {
int a;
static A foo() { return {5}; }
...
0
votes
2
answers
111
views
Calling move constructor when returning an rvalue-reference
This is a follow-up question of my previous question.
Consider the following toy code:
#include <iostream>
using namespace std;
class X
{
public:
X() { }
X(X&& x)
{
...
5
votes
1
answer
310
views
Why passing a string literal to a template calling std::format fails to compile?
The following code snippet fails to compile on the latest version of MSVC (Visual Studio 2022 17.2.2). The same snippet seemed to work just fine on previous compiler versions.
#include <iostream>...
0
votes
3
answers
119
views
Why is static_cast<Object&&> necessary in this function?
Trying to understand std::move, I found this answer to another question.
Say I have this function
Object&& move(Object&& arg)
{
return static_cast<Object&&>(arg);
}
...
1
vote
1
answer
102
views
Timing a class function in C++
A previous post, Timing in an elegant way in C, showed a neat method for profiling using a wrapper function. I am trying to use one of the profiler to profile my class functions.
#include <cmath>...
3
votes
1
answer
77
views
Inserting non-const pair into `std::unordered_map` is slower than const pair
I have some code like these (from cppcon), when inserting a non-const pair into a unordered_map, the performance is very different to inserting with a const one.
#include <algorithm>
#include &...
0
votes
0
answers
38
views
How to make a function that returns an rvalue reference to primary data type? [duplicate]
Here's a short snippet.
#include <iostream>
using namespace std;
int&& add(int& a, int& b) {
return move(a + b);
}
int main(void) {
int a = 1;
int b = 2;
cout &...
1
vote
3
answers
112
views
why non-movable Object still a copy
Consider the following code, Entity object is non-movable. I know that std::move(Obj) just cast the Obj to a rvalue reference Obj. And I also know that rvalue reference variable is still a lvalue ...
-1
votes
1
answer
56
views
Regarding R values
#include <iostream>
#include <ctime>
#include <chrono>
#include "Person.h"
#include "SmartPerson.h"
using namespace std;
void print_name(const Person& test);
...
0
votes
3
answers
990
views
no matching function for call to <unresolved overloaded function type>
I can't relate with similar questions. That's my MRE, basically I'd like to overload fun with a version accepting a template reference. It all works until std::thread enters in the game. It seems I'm ...
0
votes
2
answers
726
views
C++: Pass string literal or variable to function
I have a function f that takes a string as input. I usually want to provide a string literal, e.g., f("hello"). However, I want to implement another function g that builds upon f:
std::...
1
vote
2
answers
74
views
Transparent passing C++ variadic call parameters to ostream output operator
I have written myself the following function:
template <class Stream>
inline Stream& Print(Stream& in) { return in;}
template <class Stream, class Arg1, class... Args>
inline ...
3
votes
2
answers
232
views
std::forward not passing lvalue reference
I am probably missing something basic here, but why don't I get a lvalue from std:forward bellow?
#include <iostream>
#include <utility>
template <typename T>
void f(const T& ...
0
votes
0
answers
15
views
C++ reference to rvalue stays valid [duplicate]
I have a class Foo that store a const reference to a Bar. If I pass a temporary Bar to the constructor, I thought that storing that would be bad, but it works correctly. Does storing the rvalue ...
0
votes
2
answers
122
views
C++ pass by reference tricky situation
I'm trying to figure what happens to an "rvalue",temporary object, after the variable used to refer this object deleted from stack.
Code example:
#include<iostream>
using namespace std;...