Questions tagged [pass-by-rvalue-reference]
37 questions
33
votes
3 answers
Does the C++ standard guarantee that a failed insertion into an associative container will not modify the rvalue-reference argument?
#include
#include
#include
using namespace std::literals;
int main()
{
auto coll = std::set{ "hello"s };
auto s = "hello"s;
coll.insert(std::move(s));
assert("hello"s == s); // Always OK?
}
Does the C++…

xmllmx
- 39,765
- 26
- 162
- 323
10
votes
3 answers
Why can't I pass an rvalue-reference as it is to another function in C++11?
I have a code:
void f(int&& i) {
auto lambda = [](int&& j) { (void)j; }
lambda(i);
}
int main() {
f(5);
}
Clang++ gives an error: no known conversion from 'int' to 'int &&' for 1st argument
Why the i changes its type to int when being passed…

abyss.7
- 13,882
- 11
- 56
- 100
10
votes
1 answer
Pass by value or universal reference
I want to develop a small polymorphic class with type erasure and I wonder which version of the templatized constructor is better and should be used.
We can pass by value:
class A
{
...
template< typename T >
A( T t ) { /* create the…

headmyshoulder
- 6,240
- 2
- 20
- 27
10
votes
4 answers
prevent pass-by-ref of temporary object
I have a class that 'remembers' a reference to some object (e.g. an integer variable). I can't have it reference a value that's destructed immediately, and I'm looking for a way to protect the users of my class from doing so by accident.
Is an…

xtofl
- 40,723
- 12
- 105
- 192
7
votes
3 answers
Returning an argument passed by rvalue reference
If I have a class A and functions
A f(A &&a)
{
doSomething(a);
return a;
}
A g(A a)
{
doSomething(a);
return a;
}
the copy constructor is called when returning a from f, but the move constructor is used when returning from g. However, from…

Jeremy B.
- 81
- 1
- 7
6
votes
2 answers
Can I forbid temporary objects as parameters?
Let's say I have the function:
void foo(Object& o) {
/* only query o, dont alter it*/
}
Is it possible to call this function only with already constructed objects and have Visual Studio throw a compile error if I call the function with a…

Raildex
- 3,406
- 1
- 18
- 42
6
votes
2 answers
Do rvalue references have the same overhead as lvalue references?
Consider this example:
#include
// runtime dominated by argument passing
template
void foo(T t) {}
int main() {
int i(0);
foo(i); // fast -- int is scalar type
foo(i); // slow -- lvalue reference…

Taylor Nichols
- 588
- 2
- 13
5
votes
1 answer
(c++23 implicit move) Returning the moved local storage variable as an rvalue ref with only parenthesisses?
Regarding the proposal "Simpler implicit move" (P2266R1), I'm not sure if I understand this new "move-eligible" things correctly.
Please correct these points if incorrect:
[LIVE]
std::forward becomes optional for perfect forwarding the rvalue ref…

sandthorn
- 2,770
- 1
- 15
- 59
5
votes
1 answer
Custom type conversion operator doesn't work when invoked on a forwarding reference (works when object is passed by value)
I can't fathom the essence of this error so pardon me if the title could be better. This code fails to compile:
template
struct value_as_type {
using type = decltype(v);
static constexpr type value {v};
constexpr operator…

Violet Giraffe
- 32,368
- 48
- 194
- 335
4
votes
2 answers
Why the rvalue reference parameter cannot be passed between functions directly?
My code is as follows:
#include
using namespace std;
class A{
public:
void sendByRvalue(string&& str){
cout << str << endl;
}
};
class B{
private:
A a;
void send(string&& str){
a.sendByRvalue(str);
…

Phoenix Chao
- 390
- 3
- 19
4
votes
1 answer
Return rvalue reference vs return by value in function return type
In my code I have a function that constructs a string from a piece of data and then returns it. This string isn't used anywhere else, so it's safe for the receiving side to use move-assignment or move-initialization on it.
std::string…

Samuel Moriarty
- 958
- 1
- 8
- 20
4
votes
1 answer
Confusing error messages with named rvalue references
Consider the following:
struct my_type {};
my_type make_my_type() { return my_type{}; }
void func(my_type&& arg) {}
int main()
{
my_type&& ref = make_my_type();
func(ref);
}
Needless to say, this code doesn't compile. I realise that I…

Tristan Brindle
- 16,281
- 4
- 39
- 82
3
votes
1 answer
Why does C++ give preference to rvalue reference over const reference while function call?
So I wrote a code in C++ 11
#include
using namespace std;
void print (int &&a)
{
cout<<"rval ref";
}
void print (const int& a)
{
cout<<"const ref";
}
int main()
{
print(9);
}
The output of code was fascinating that it…

simplekind
- 39
- 4
3
votes
2 answers
Passing only value for private members
I am creating a class with one array as its private member and getter,setter method for that. I want set a value to that array using an array in main function. When I manipulate the array in main function, it should not affect the array present in…

Shaheen Ahamed S
- 176
- 2
- 11
3
votes
4 answers
What is the most efficient way to set class variable using rvalue in c++?
I just started working with c++11 r-values. I read some tutorials, but I haven't found the answer.
What is the best way (the most efficient way) to set a class variable? Is below code correct or not? (let's assume std::string has defined move…

marco
- 111
- 2
- 8