Questions tagged [rvalue]

An rvalue is a temporary object (or subobject) or is a value not directly associated with an object.

Traditionally known as r-values since they generally appear on the right hand side of an expression; rvalues are temporary object whose lifetime does not extend past the current expression (unless bound to a an appropriate reference see ).

An rvalue is not directly associated with an object. Often a simple test is applied to determine if is an rvalue; "Is it named?"; if so, it is not an rvalue.

C++11 extended the original definition of an rvalue to include prvalues (pure rvalues) and xvalues (expiring values).

For a more general post on value categories, see this post on SO.

718 questions
0
votes
0 answers

An rvalue reference can only bind to non-const rvalues?

Previous post indicated a rvalue reference can only bind to non-const rvalues: Difference between returning a const reference and rvalue reference However, if that's true, how perfect forwarding works? For this definition for perfect…
lightrek
  • 951
  • 3
  • 14
  • 30
0
votes
2 answers

Compilation error concerning operator[]

I created a template class vect that allows me to create arrays of elements of type T, access them from 1 to n (instead of 0 to n-1) and permute them (I have to permute them that way instead of permuting them the classical way). Here's the header…
Scientifica
  • 169
  • 1
  • 10
0
votes
1 answer

Pass a type name to a templated operator

I know that there are questions with the same topic, but this one asks for something more specific. I am creating a personal use multi-type templated container. The details are not important, but here are basic operators that I wanted to use with my…
KKZiomek
  • 244
  • 1
  • 2
  • 12
0
votes
0 answers

Enforce operator version from lvalue/rvalue context

I'm trying to make some nice syntax for assignment of array slices using operator overloading. Due to some peculiarities with const-correctness of references, different classes are needed for lvalue and rvalue slices. However, I cannot get the…
kalj
  • 1,432
  • 2
  • 13
  • 30
0
votes
1 answer

Performance of move constructor of object with tuple member

I have a question about performance of move constructor, pseudo C++ class: typedef tuple, ...and more...> FooTupleMember; class Foo1 { public: Foo1::Foo1 (Foo1&& object) : member (std::move…
Goofy
  • 5,187
  • 5
  • 40
  • 56
0
votes
4 answers

Efficient custom string creator

I'd like to know what's the most effective way of passing a custom string. For example, I have this code segment: outputFile << addSpace(data.len()); where string addSpace(int n) { string result(""); for (int i = 0; i < n; i++) { …
Eliran Abdoo
  • 611
  • 6
  • 17
0
votes
0 answers

Why int& rx = ... does not require rvalue on the right hand side

I read a lot of rvalue lvalue related questions and blogs, and now I have the following question. When assigning some object to reference variable, why is it OK that the right-hand-side is not rvalue. int x = 11; int& rx = x ; // x is lvalue. I…
Toshihiro
  • 61
  • 1
  • 1
  • 7
0
votes
1 answer

universal reference for template type error

Excuse the bad title... So, normally in a function like this: template void f(T&& i){ } T&& is a universal reference. In a context like this it is an rvalue-reference: template struct s{ void f(T&& t){} }; This makes…
lo tolmencre
  • 3,804
  • 3
  • 30
  • 60
0
votes
0 answers

push_back a PRvalue into a vector

Carefully reading the references, A move constructor can only be called by a Xvalue. Then, how something like... std::vector vec vec.push_back(Class(..args..)) is possible without moving(casting) it to an Xvalue? Is there a specific way to…
Ray Kim
  • 33
  • 2
  • 8
0
votes
1 answer

C++ generic rvalue overload

I'm currently trying to refactor some code which uses a primitive type into something which I can tuck setters and getters into them. I wanted to make the change to the underlying code transparent, and at first I though C++'s operator overloads…
MVittiS
  • 434
  • 3
  • 13
0
votes
1 answer

Prevent passing temporary object as an argument to a function

Suppose I have this: template class StringView { private: const CharType* m_String; size_t m_Length; public: template inline StringView(const…
Sunius
  • 2,789
  • 18
  • 30
0
votes
1 answer

passing Rvalues into a copy constructor and assignment operator

Passing an Rvalue into a copy constructor or assignment operator seems to me to be a very important thing to be able to do. For example: int a = b+c; or int a; a = b+c; Without this it would be hard to do math calculations. Yet I am unable to do…
Mathew
  • 1,116
  • 5
  • 27
  • 59
0
votes
1 answer

Move into a function that takes const lvalue reference

I am using this class, not written by me and I can't change its code: class A { private: Val d_val; public: void setVal(const Val& val) { d_val = val; } const Val& getVal() const { return d_val; } }; Val supports move but class A was…
ytoledano
  • 3,003
  • 2
  • 24
  • 39
0
votes
0 answers

C++ Invalid Initialization Error

I'm trying to compile something like this: int f(int a){ return a+2; } void g(int& a){ a++; } int main(){ int a=5; g(f(a)); } but I get the error: error: invalid initialization of non-const reference of type 'int&' from an…
sworwitz
  • 19
  • 7
0
votes
3 answers

Returning const lvalue reference to rvalue temporary? Why does this work?

Why does this code work? It prints out 60 every single time. First of all, const thingy& indicates that the function returns a reference to an already existing variable, not a nameless construction. Secondly, shouldn't that temporary die when the…
Sus Among Us
  • 103
  • 2
  • 11