Questions tagged [rvalue]
An rvalue is a temporary object (or subobject) or is a value not directly associated with an object.
718
questions
1
vote
1
answer
131
views
Incrementing rvalue in C++ [duplicate]
Does anyone know why this code does not compile
int main() {
int i = 7;
int j = ++i++; // error: lvalue required as increment operand
}
while this code compiles perfectly?
int main() {
Int i{7};...
2
votes
2
answers
109
views
What does is_constructible_v<std::string&&, std::string&&> mean by?
I can understand what is is_constructible_v<std::string, std::string&&>.
But what does is_constructible_v<std::string&&, std::string&&> mean by?
What is difference ...
4
votes
1
answer
86
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:
...
3
votes
0
answers
22
views
How to correctly write separate methods for rvalue and lvalue overloads?
I have the following code.
#include <iostream>
#include <string>
struct A {
int x;
int y;
};
struct B {
A a;
~B(){
std::cout << "deleted" << ...
2
votes
1
answer
75
views
How to call rvalue methods of members?
I'm writing a class (Interface), which encapsulates a class (Impl) which happens to have an rvalue function (see Impl::Close).
class Impl
{
public:
/// Called from Dtor.
/// You can't reuse ...
0
votes
1
answer
43
views
I have a question depending on the location of 'const' in C++
int i = 0;
int* lv1 = &(++i); //++i is lvalue
//int* rv1 = &(i++); //i++ is rvalue
const int* &rv1 = &i;
int* const &rv2 = &i;
int* &&rv3 = &i;
const int &rv4 = ...
2
votes
1
answer
104
views
What are the rules of rvalue(&&) lvalue(&) reference binding in templates with regard to reference collapsing?
Consider these cases:
int i{};
int& ir{i};
class A{
public:
int& i;
A(int&& pi):i(pi){}
};
A a1{i}; // Error // case 1
A a2{int(1)}; // OK // case 2
class B{
public:
int&...
0
votes
3
answers
79
views
How to implement a make_rv function in c++
I want to write a template which can turn lvalue/rvalue to rvalue using universal reference like std::forward
For rvalue, just forward it.
For lvalue, copy it.
it is used as below
template<typename ...
2
votes
1
answer
91
views
Why the ownership of unique_prt is not transfered when using rvalue reference?
If I run the code
class Myc {
private:
int m{0};
public:
Myc(int ii) : m{ii} {};
~Myc() { std::cout << "Myc Destructed\n"; }
int getM() const { return m; }
};
...
1
vote
1
answer
56
views
An error occurred while constructing a 3D vector class
I'm debugging a 3D vector code, but I get the following error
/usr/bin/g++ -fdiagnostics-color=always -g /home/fangrui/vectorFEM/vectorFEM/main.cpp -o /home/fangrui/vectorFEM/vectorFEM/main
In file ...
1
vote
1
answer
49
views
Template specialization for rvalues
I have a compact set of functions that I use to write arbitrary data to a comma separated values file. It looks something like this:
template<typename T>
void log(std::ostream& out, T a) {
...
-2
votes
1
answer
89
views
What is faster to compute in C? (x==0) or (0==x)? [duplicate]
I wonder what is better to compute in C language:
if (x==0)
{
// Some code ...
}
of
if (0==x)
{
// Some code ...
}
I know the last is better in case the programmer forgets the second "...
0
votes
3
answers
86
views
How to do: non const member function used on objects left side and const member function used on objects right side of assignment
Consider the following C++ code:
class CBuffer
{
private:
char Array_L[10];
char Array_R[10];
public:
const char& AccessByte(int index) const { return Array_R[index]; }
char& ...
2
votes
1
answer
111
views
What does the "lvalue to rvalue" conversion mean for std::decay?
I am confused about the wording for std::decay on cppreference. They say it applies "lvalue to rvalue." I know std::decay removes referenceness, so is this what they mean by that? If so, why ...
0
votes
0
answers
102
views
Is there a way of preventing a C++ compiler from optimizing a copy of a temporary rvalue into a move operation?
eg:
std::vector<int> i;
i = std::vector{5, 4, 3, 2, 1}
In the above case, a compiler will typically optimize the second line into a move operation instead of a copy operation despite the lack ...
-1
votes
1
answer
166
views
Non-member operator: cannot bind non-const lvalue reference to an rvalue
Binding a non-const rvalue to a rvalue with member operators work (case A), but binding to a non-member operator (case B) does not:
struct A
{
A & operator<<(int i) { return *this; }
};
...
3
votes
2
answers
109
views
range parameter for any iterable input with properly convertible elements
I am currently trying to get used to C++20 especially concepts and ranges in this case. Hopefully the title fits my problem I am not really sure yet what I ran into.
I want to create a set method for ...
4
votes
2
answers
115
views
Why doesn't make_pair<string, string>() call the copy contructor, when given const string&? [duplicate]
Both GCC and Clang refuse to compile this one:
#include <string>
#include <utility>
using namespace std;
int main() {
const string s = "12345";
const string& r = s;
...
2
votes
1
answer
40
views
How does the system know about the type of address it is pointing to in case of a rvalue?
I have some doubt (possibly a misconception) regarding the concept of rvalues when it comes to pre-increment or post-increment. Consider the following C program:
#include <stdio.h>
int main() {
...
3
votes
1
answer
149
views
Why is it ok to assign lvalue to rvalue in C++? [duplicate]
string s = "hello";
s + s = s; //compiles with no error/warnings
I was expecting a compilation error given rvalues are temporary values and can't be appear as an assigned value in an ...
1
vote
3
answers
137
views
c++ 20: how to find that the template argument is a rvalue reference
I want to pass a parameter to a function, do for, and if the argument is rvalue, then move objects instead of copying. How to do that?
template<class T> void func(T&& v){
for (auto&...
1
vote
1
answer
140
views
Move semantics with std::vector
I have a question concerning the move semantics in C++. See this example:
class Buck {
public:
Buck(std::vector<int> param) : data{param} {}
std::vector<int> data;
};
int main() {...
6
votes
1
answer
157
views
Ambiguous overload with different user cast operator for lvalue and rvalue cases
I try to compile this test code
struct MyData { /*..*/ };
template < typename T >
struct Wrapper
{
T m_value;
operator T const & () const & { return m_value; }
operator T &...
1
vote
4
answers
109
views
Why I can take address of *v.begin() where v is a std::vector
#include <vector>
#include <cstdio>
using namespace std;
int f()
{
int* a = new int(3);
return *a;
}
int main()
{
//printf("%p\n", &f());
vector<int> v{...
-1
votes
1
answer
157
views
Why std::move doesn't avoid a second destruction?
I know ordinary std::vector::push_back() will copy the object. I hope this code would only destruct a only once, using std::move() and A(A&&) noexcept to avoid copying. But it doesn't seem to ...
1
vote
1
answer
38
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
80
views
rvalue reference -what is new with 'move constructor' that could not have been implemented in C++98?
I am new at C++ 11/17 and trying to understand how the rvalue reference move works.
In the code below what is the difference between the "modified copy constructor" and "move ...
0
votes
2
answers
98
views
Why do I get this lvalue & rvalue error while using a void function in C++? [duplicate]
I'm working on this exercise 18.7 in C++ Primer Plus (6th edition):
and the code I gave was like this, using the lambda expression as required:
#include <iostream>
#include <array>
#...
1
vote
0
answers
11
views
Forwarding rvalue into a thread where it is further forwarded to another function
I'm trying to create a deadline class which starts a thread in its constructor. Once the deadline (a time priod) occur within a thread, a function provided as argument should be executed.
The class'es ...
0
votes
0
answers
26
views
why do i get this compile error: " error: cannot bind non-const lvalue reference of type ‘Matrix&’ to an rvalue of type ‘Matrix’ "?
Ok so i'am writting this code for an exercise we have to write a class for matrices and then to check it we use a code that the teacher wrote to check if it performes the normal operations.
And i don'...
5
votes
1
answer
223
views
Is there a technical reason why range-based for loop doesn't detect whether it's looping on an rvalue?
The reason for the question is that I've seen code like this:
auto fun(std::vector<Foo>&& v) {
std::vector<Bar> w;
for (auto&& e : v /* not an rvalue, but keep ...
0
votes
2
answers
305
views
C++ class member rvalue assignment
I have seen C++ code that assigns a class member using an std::move call on an rvalue as follows:
class Widget {
std::vector<int> m_data{};
public:
// 1. OK
// x contents are copied ...
0
votes
1
answer
149
views
How to use an abstract class rvalue reference member?
I have an abstract class Base and derived class Derived:
class Base
{
public:
Base(int n) :_n(n) { _arr = new int[n]; }
virtual ~Base() { delete[] _arr; }
Base(Base&& other) { _n = ...
1
vote
1
answer
235
views
Return a shared pointer from a function vs Capturing a shared pointer in a Lambda
I am constructing a shared pointer in a function_1 and giving it as a capture to a lambda.
I think this is an issue, could you please confirm if this safe or I am right and I shoudn't be doing this?
#...
2
votes
1
answer
102
views
error use of deleted function when trying to pass rvalue to a tuple
Original context:
I am trying to pass a tuple of (object, expected_value_of_some_property) to a test function
I created a simple class to reproduce the error I am facing:
template <typename T>
...
0
votes
1
answer
739
views
cannot bind non-const lvalue reference of type 'int*&' to an rvalue of type 'int*'
I am aware that there are some questions similar to this one but I am a beginner in c++ and those examples were a bit difficult to comprehend for me. In my problem, I have a function called void ...
0
votes
0
answers
47
views
Confusion about overloaded function when there is an rvalue reference argument [duplicate]
If there is an rvalue reference in a function overload, how to call to function?
Why not call the version of the rvalue reference?
void test(int& x) {
cout << "int &x" <&...
-1
votes
1
answer
19
views
Deallocating resources through move semantics
I came across codebase, where "moving ownership" through move semantic is used very frequently to deallocate resources. Example:
void process_and_free(Foo&& arg) {
auto local_arg = ...
58
votes
1
answer
2k
views
std::is_same different results between compilers
#include <iostream>
int main() {
bool b = true;
std::cout << std::is_same<decltype(!(!b)), bool>::value << "\n";
auto bb = (!(!b));
std::cout <&...
0
votes
1
answer
105
views
How to pack std::thread with template constructor and lambda function?
Why is data in func() repeated?
#include <memory>
#include <thread>
#include <unistd.h>
#include <vector>
struct Data {
int id;
};
class Thread {
public:
...
0
votes
1
answer
83
views
C++11: passing a temp object to a ctor, doesn't call move constructor, why? [duplicate]
I was trying to see why move constructor is called, as below:
#include<iostream>
#include<utility>
using namespace std;
struct M{
M(){cout<<"ctor\n";}
M(const M&m){...
1
vote
1
answer
178
views
C++11 recursive function using "&&" rvalue reference leads to compilation error
I tried to write some algorithm using recursive function and met this:
#include<vector>
using namespace std;
void f(vector<int>&& v) {
if (!v.empty()) {
// do some work
...
-1
votes
1
answer
31
views
Is temporary object initialization expresssion implicitly converted to xvalue while we access the this pointer?
The code is:
class A {
public:
void f() { cout << this; }
};
int main() {
A{}; // prvalue
A{}.f(); // Is A{} in here converted to xvalue?
}
On https://en.cppreference.com/w/cpp/...
3
votes
0
answers
86
views
How to determine programmatically if an expression is prvalue or lvalue or xvalue in C++? [duplicate]
The original question is here: How to determine programmatically if an expression is rvalue or lvalue in C++?
My question is, we can already distinguish between lvalue and rvalue, so can we go further ...
-3
votes
1
answer
138
views
Why is move assignment of unordered_map slow?
I am trying to understand how the move/rvalue assignment operator works. I know that it is largely implementation-specific, but assuming that move assignment in unordered_map works by only swapping ...
-1
votes
1
answer
82
views
lvalue reference on rvalue reference
I have an interesting example to understand lvalue reference, rvalue reference, and std::forward. Maybe it will be a useful example for a deep understating concept.
void foo(int&& a){
cout&...
3
votes
1
answer
213
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<...
1
vote
0
answers
112
views
Why does rvalue ostream operator<< not use perfect forwarding?
Overloads for streaming objects into std::ostream and friends tend to only be written for lvalue ostreams.
Because it can be convenient to write code like std::ofstream("myFile.txt") <<...
1
vote
2
answers
195
views
Why can I assign from an rvalue twice in C++? [duplicate]
I was reading the chapter 13 of C++ Primer when I read :
"It is essential to realize that the call to move promises that we do
not intend to use [an rvalue] again except to assign to it or to
...
0
votes
0
answers
30
views
Why can't we modify an l-value reference from an r-value? [duplicate]
You can have an lvalue reference on an rvalue, but the lvalue must be const. Look at this simple program:
#include <iostream>
int compute()
{
return 7*4;
}
int main(int a, char**b)
{
...