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,098
questions
-2
votes
1
answer
57
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
128
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 ...
12
votes
3
answers
198
views
Integer parameter calls float overload and float parameter calls integer overload? [duplicate]
Today i ran into roughly the following code:
#include <iostream>
void f(float&& f) { std::cout << f << "f "; }
void f(int&& i) { std::cout << i <&...
-1
votes
0
answers
88
views
Why did compiler add static_cast to const rvalue ref if an object has a user defined/declared destructor
As per following example
struct Apple {
~Apple() {};
};
int main()
{
Apple a1;
Apple a2 = std::move(a1);
}
and example
struct Apple {
~Apple() = default;
};
int main()
{
Apple a1;
...
2
votes
1
answer
64
views
Cast to rvalue reference prevents copy elision
I read more related articles, but none of the answers clarified my doubts. Why in the code below the optimization takes place only when there is no cast to an rvalue reference, otherwise either the ...
-2
votes
3
answers
194
views
How to efficiently return an object when copy/move elision is disabled?
Assuming copy elision is disabled when compiling, is the following a valid thing to do to avoid unnecessary copying (simulating copy elision)?
Container getContainer() {
Container c;
return c;...
3
votes
2
answers
167
views
Why does C++ prohibit binding T1 && to T2 lvalue when T1 is reference-related to T2 (and T1 and T2 are not class types)?
I'm exploring this question: Given char * x, why doesn't const char * && rc2 = x compile?
And I found dcl.init.ref#5.4.4 which says (when we are binding T1 && to T2 lvalue where T2 is ...
2
votes
1
answer
97
views
Is it unsafe to return a variable by `std::move` in stack? (Compiler shows warning for this)
I read many answer that local variable can be safely returned by std::move.
But when I tried it, compiler shows warning like below.
class MyClass {};
MyClass&& func() {
MyClass c;
...
1
vote
3
answers
173
views
using std::forward on the same argument in a loop
I know there are cases where using std::forward on the same argument in a loop is wrong because it may cause moving from a moved object, like so:
template <typename T>
auto applyTenTimes(T&&...
1
vote
1
answer
98
views
What's causing the error in this example using a const rvalue reference parameter?
Using this program:
#include <sstream>
#include <iostream>
static std::ostringstream oss;
struct fmt
{
template <typename T>
fmt& operator<< (const T&& val)
...
0
votes
1
answer
65
views
Why std::forward return a rvalue when transferring a lvalue? [duplicate]
It is said that std::forward() perfectly forwards a lvalue to lvalue, and rvalue to rvalue. But the code below seems to show a different conclusion?
when I run the code below
#include <iostream>
...
0
votes
1
answer
54
views
Forwarding reference does not deduce to rvalue reference [duplicate]
First, this question has been asked by others, see this and this, but (I think) neither of them provides a satisfactory answer.
According to cppreference,
Before deduction begins, the following ...
-2
votes
1
answer
83
views
Why move semantics in C++ have rvalue reference in function signature [duplicate]
Consider the below move assignment operator:
class MyClass {
private:
ssize_t buf_size;
void* buf_ptr;
public:
MyClass &operator=(MyClass &&rhs) {
if (this != &rhs) {
...
3
votes
1
answer
107
views
Is the rvalue passed to the function still rvalue or not?
I'm trying to understand everything I can about transfer semantics, rvalue, xvalue, prvalue, etc.
I have the following function:
void f(int&& x, int i)
{
std::cout << boost::...
-1
votes
1
answer
51
views
Pass parameter pack to function with template deduction from return type
I'm writing an object allocator that I'd like to call in the following way:
T result = factoryObject.construct(argA, argB, argC);
I currently have this design, which works...
class Factory {
void* ...
2
votes
4
answers
149
views
Why are string literals lvalues and not xvalues?
Ben Saks in his lesson "Understanding Value Categories" at the 2019 CppCon in Aurora (CO) (great lesson btw) said:
"Character string literals, such as "examplestring", are ...
0
votes
0
answers
22
views
what's the difference between call by value and call by rvalue reference in vector.push_back(const & T val) before c++11
Since rvalue references were not supported before C++11, there were no move constructors. The const T& arguments makes it use the copy constructor even for temporaries, which is the same as ...
4
votes
1
answer
94
views
Why rvalue references behave differently based on types?
I'm reading about C++ rvalue references and value categories for expressions. However, I'm not able to grasp (from a lvalue, xvalue, prvalue point of view) why the following code works as expected:
...
-1
votes
1
answer
126
views
In the C++ 20 specification, where is the rule saying an rvalue reference cannot bind to an lvalue?
Of course, this will not compile:
struct X {};
X&& x = X();
X&& x2 = x; // error: rvalue reference to type 'X' cannot bind to lvalue of type 'X'
Where is the relevant rule to this in ...
2
votes
1
answer
139
views
Function that can receive any of T, T&, and T&& as input, AND also recognize its type?
I want to create a function that correctly recognizes the type of its parameter.
template<class T> void test(T&& t){
if constexpr(std::is_same_v<T,int>){
std::cout<&...
4
votes
2
answers
98
views
Why binding rvalue to a const type makes it an lvalue?
This question may not have the best title but here is the code that will explain what I am trying to ask.
This code runs and prints "lvalue" but If I remove const from MyPair's first type, ...
0
votes
0
answers
7
views
Function Overload Resolution - Between Const Lvalue Ref and Rvalue Ref
I came across the following C++ code -
class C {
public:
C() { std::cout << "1"; }
C(C const&) { std::cout << "2"; }
C(C&&) { std::cout << "...
-2
votes
1
answer
53
views
C++ template function and universal reference error?
What is the exact difference of universal reference and rvalue reference?
The code below should except both rvalue reference and lvalue reference as argument. And apparently the first function ...
1
vote
1
answer
95
views
Binding lambda to forwarding reference
Is it safe to bind to a lambda with auto&&? I.e will the lifetime of the lambda be extended to the lifetime of the forwarding reference? Why / why not?
auto&& fn = <lambda ...
4
votes
2
answers
258
views
What does it mean to have an rvalue reference variable?
I think I understand functions with signatures like:
void f(std::string&&):
It will be applied to rvalues to reuse their resources. However, I've seen occasionally code like:
std::string t();
...
7
votes
1
answer
77
views
Unexpected output when invoking overloaded functions with different rvalue reference types
I'm encountering an unexpected behavior in my code when calling overloaded functions with different rvalue reference types. The code snippet below demonstrates the issue:
#include <iostream>
...
2
votes
2
answers
107
views
Understanding std::move and Ownership Transfer with std::unique_ptr in C++
I have written two sets of functions, F1/test1 and F2/test2 and I'm trying to understand the differences in the behavior of std::move and the transfer of ownership with std::unique_ptr in these ...
3
votes
0
answers
87
views
When does an rvalue reference result in a move vs copy constructor and why? [duplicate]
I am confused about explicit usage of the rvalue reference.
Suppose we have a class named ClassX and it has all copy and move methods. When I execute the code below, different copy methods have been ...
0
votes
0
answers
26
views
The correct way to handle smart pointers in Dependency Injection in C++ [duplicate]
I'm implementing the Dependency Injection pattern in C++ with smart pointers. When using std::unique_ptr I'm wondering if there is any difference passing pointers in the constructor by rvalue ...
0
votes
1
answer
110
views
Problem with storing forward references(universal) to be forward and used later
Problem with storing forward references(universal) to be forward and used later
I'm writing a class where I can pass any function pointer with its argument types and return type, and later call the ....
6
votes
1
answer
130
views
Strange rvalue reference in clang [duplicate]
The following code:
#include <iostream>
struct A {
A() { std::cout << "()" << std::endl; }
A(A&&) { std::cout << "(A&&)" << std:...
0
votes
2
answers
64
views
A templated function to always retrieve the value of T, being T an universal reference
I want to have a helper function that helps me to get the value or the value of the pointee when its a pointer.
For example, I want to substract a pair numerical values or the values for the types ...
2
votes
1
answer
72
views
Can `&&` inside a higher order function parameter be a forwarding reference?
The canonical example of a forwarding (or universal reference) goes like so:
template<typename T>
void f(T&& param);
i.e.
"If a variable or parameter is declared to have type T&...
5
votes
2
answers
102
views
Why rvalue reference template variable is able to bind to lvalue
Why rvalue reference template variable b<int> is able to bind to lvalue a?
#include <iostream>
int a = 3;
template<typename T>
T&& b = a;
int main() {
if(std::...
0
votes
1
answer
84
views
Returning a temporary argument in C++: Identity function
In C++, I can write a statement std::string& x = f();. If f() returns a std::string (not a reference), it will extend the lifespan of the temporary variable until x goes out of scope.
What if I ...
3
votes
0
answers
97
views
What is the difference between rvalue and lvalue references to functions? [duplicate]
From c++11 onwards, there are lvalue and rvalue references. I am aware of the differences between them. However, akin to function pointers such as int(*fptr)(), c++ also has lvalue and rvalue ...
2
votes
0
answers
180
views
Importance of Universal Reference in C++
I started reading books and blog posts by Scott Meyers to improve my C++ skills. I read the blog about Universal References in C++, and I'm trying to understand it.
Is the reason for the existence of ...
0
votes
0
answers
57
views
C++ Storing a rvalue reference through a shared_ptr
I have a method which takes an rvalue-reference. I want to store a pointer/reference to this object so that it stays in memory and I can access it again later.
Specifically, I'm working on a tensor ...
-1
votes
1
answer
128
views
How to perfectly forward a universal reference that is either a const ref or a movable rvalue?
I have coded a lock-free and thread-safe ring queue with C++20, and it works so far. The only thing is not perfect that it has to have two enque() methods, one accepts a const reference to a lvalue as ...
2
votes
1
answer
106
views
Reference to global functions not deduced correctly
I am using gcc 12.2 and find that the following code compiles and produces weird results try it in Godbolt. (P.S. Switching to clang shows the same result)
#include <iostream>
void global() { /*...
0
votes
1
answer
65
views
Reviewing the implementation of a function that splits character types
I needed to have a helper for splitting a string by a delimiter. So I've wrote this code:
import std;
template <typename T>
concept SplittableString =
std::is_same_v<const char*, std::...
1
vote
1
answer
87
views
In standard algorithms, why are some template parameters rvalue references while others are not?
For example, let's take a look at the signature of std::reduce():
template< class ExecutionPolicy, class ForwardIt, class T, class BinaryOp >
T reduce( ExecutionPolicy&& policy,
...
0
votes
1
answer
332
views
"Transfer" to function in C++: pass by value or rvalue reference?
This question discusses passing by value vs. passing by rvalue reference in C++. However I find the answers unsatisfactory and not entirely correct.
Let's say I want to define a Queue abstract ...
2
votes
1
answer
71
views
The predicate functor in std::find_if is not reference
Following is the definition of std::find_if
template< class InputIt, class UnaryPredicate >
constexpr InputIt find_if( InputIt first, InputIt last, UnaryPredicate p );
Why doesn't it use ...
4
votes
1
answer
114
views
Why can I bind base class shared_ptr rvalue reference to derived class shared_ptr?
so I know we cannot bind rvalue reference to an lvalue, so the following code won't compile:
class Base {};
int main(int argc, char** argv) {
std::shared_ptr<Base> base = std::make_shared&...
2
votes
1
answer
215
views
r-value reference as a class member
test<long double&> contains a reference so test<long double&> has the size of a pointer.
I believed that test<long double> contains long double and therefore, I expected it ...
0
votes
0
answers
73
views
Why not always take by rvalue ref?
I have an API which just enters a subscription into a vector of callbacks. The vector uses std::function which could be partially heap allocated, hence move operations on std::function make sense. Now ...
1
vote
1
answer
39
views
Providing a template type of the same type as argument causes an error for rvalue reference function
I'm using a library with a templated function that takes an rvalue reference, and for clarity, I am explicitly stating the template type instead of relying inference. I noticed that when the template ...
2
votes
1
answer
61
views
Moving parameter to data member: Take parameter by copy or rvalue-ref?
I have a class object entity which gobbles up a string and shoves it into it's member on construction (for the sake of this argument this could be any old member function).
Now I can do this in at ...
0
votes
0
answers
49
views
What is ONE possible use case for the const&& method qualifier? [duplicate]
I award 100 reputation to the person who can show me one sensible example of how a const && method-qualifier could be used in real world code which cannot be done more elegantly by using just ...