64 questions
0
votes
0
answers
33
views
Extend life of temporary using rval ref to ternary operator fails for non copyable types [duplicate]
consider:
struct S
{
S() = default; // ✓
S(S const&) = delete; // copy ✗
};
void f(S* a)
{
S&& s = a ? *a : S{};
}
int main()
{
f(0);
}
https://godbolt.org/z/s7v9v7Mdo
...
0
votes
1
answer
123
views
What is an example where the result of lvalue-to-rvalue conversion removes cv-qualifiers?
Under Lvalue-to-rvalue conversion the C++23 standard says ([conv.lval]#1, emphasis mine):
A glvalue of a non-function, non-array type T can be converted to a prvalue.44
If T is an incomplete type, a ...
0
votes
1
answer
91
views
Why const reference is called and not rvalue reference and how std::move works? [duplicate]
#include <iostream>
void bar(std::string& s) { std::cout << "ref" << std::endl;}
void bar(const std::string& s) { std::cout << "const ref" << ...
11
votes
3
answers
225
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 <&...
7
votes
1
answer
88
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
1
answer
380
views
Lvalue-to-rvalue conversion C++
I read about Lvalue-to-rvalue conversion and Comparison operators
This quote from Comparison operators :
after the application of the lvalue-to-rvalue, array-to-pointer and function-to-pointer ...
-1
votes
1
answer
76
views
How to disallow increment operator when operated multiple times on a object?
How do I overload the increment operator so that this code becomes invalid -
Point p(2, 3);
++++p;
while allowing the following -
Point p(2, 3), a(0, 0), b(1, 4);
a = ++p + b;
Like in C this would ...
0
votes
5
answers
2k
views
Why do we need the address-of operator (ampersand, &) when passing a variable to scanf, but not when assigning to it?
When using the scanf function, we cannot simply specify the variable name. Rather, we have to prefix the variable name with an ampersand, e.g. int a; printf("enter your value"); scanf("...
3
votes
1
answer
121
views
Where is the rvalue coming from? [duplicate]
I learning about references and value categories because the latter are mentioned in some C++ errors.
I have a function, referenceToDouble that takes in references to double. From watching this video ...
26
votes
1
answer
1k
views
Shall structured binding be returned from a function as rvalue in C++20?
Consider a C++20 program where in function foo there is a structured binding auto [y]. The function returns y, which is converted in object of type A. A can be constructed either from const reference ...
1
vote
0
answers
288
views
rvalue and lvalue of std::string
i ran the code as shown and i am confused as to why a+b is a rvalue. From what i know, rvalue should only have a data or address and in such a case std::string should be a lvalue isn't it?
void foo(...
2
votes
1
answer
88
views
Intended invalid intialization from rvalue using *this
In C++ it is impossible to bind an r-value argument to a non-const l-value reference. But I noticed that when I call on the r-value object the method returning *this it compiles somehow.
Example:
...
4
votes
2
answers
256
views
Why the rvalue reference parameter cannot be passed between functions directly?
My code is as follows:
#include <iostream>
using namespace std;
class A{
public:
void sendByRvalue(string&& str){
cout << str << endl;
}
};
class B{
private:...
0
votes
1
answer
66
views
non-trivial rvalue and lvalue example
I am not sure about some rvalue/lvalue non-trvial examples.
are std::vector<int>({1,2,3})[0] and std::vector<int>() expressions below lvalue or rvalue?
the code has no actual usage but it ...
1
vote
1
answer
828
views
How do I pass a temporary object as a non-const reference into a member function?
We are creating a class designed to send information out from the current module (the specifics are not relevant to this question). An object of this type is created and populated with a portion of ...
1
vote
1
answer
2k
views
C2664 cannot convert to && value
The compiler wants my lvalue to be a rvalue reference and I dont see why.
My questions are:
Why is "dataLen" const, even though it was declared non const and the lambda is told to catch by ...
2
votes
0
answers
73
views
Should I explicitly =delete non-const rvalue ref qualified function?
I have a class A that has a non-const member function foo that must not be called on temporaries, this is how it looks like:
struct A
{
void foo() &
{...}
};
If foo were const I would ...
1
vote
1
answer
253
views
Clang vs G++ lvalue to rvalue conversion
The question related to this one. By tracing slt_pair. h and move. h, it's seems that the difference between Clang and G++ is internally. I have tried to simulate the assignment of the object (pair....
8
votes
1
answer
347
views
Does lvalue-to-rvalue conversion ever happen to class types?
Practically every example of lvalue-to-rvalue conversion I've seen on the web relates to fundamental types like int etc.
I couldn't find an example of l2r applicable to class types myself; in all the ...
2
votes
2
answers
113
views
Using uninitialized variable without invoking undefined behavior
From 6.3.2.1 (emphasis mine)
If the lvalue designates an object of automatic storage duration that
could have been declared with the register storage class (never had
its address taken), and ...
8
votes
3
answers
224
views
Is `(i) = 1` illegal in standard C?
I'm writing a C compiler which follows this standard, and if I parse statements like this:
int i;
(i) = 1;
my compiler will report an error which point out that (i) is a rvalue and should not be ...
2
votes
1
answer
1k
views
C++ pass vector (lvalue to rvalue)
Lets say you have 2 vectors passed to a function as lvalue references. Later you realize, you can use recursion and pass slices of these vectors using their iterators.
Would it be an appropriate ...
7
votes
1
answer
154
views
Undefined behavior inside void expressions
Is a C implementation required to ignore undefined behaviors occurring during the evaluation of void expressions as if the evaluation itself never took place?
Considering C11, 6.3.2.2 §1:
If an ...
2
votes
1
answer
824
views
What are the rules about using an rvalue reference on the left side of an equals sign?
So I've been learning about rvalues and rvalue references and have run into some code while experimenting that I can't wrap my head around the errors of.
int&& test1(int& t)
{
return ...
1
vote
2
answers
383
views
Safety of auto when std::move ing from a returned reference of a value
I need some reassurance about whenever, assigning or list initializing an auto typed named variable, with
A a std::move()ed returned reference to a variable
B a returned reference to a variable
...
1
vote
2
answers
240
views
When a function takes an rvalue reference, what is the type of that variable within the function?
This is a question of terminology. If I have this:
#include <vector>
void g(std::vector<int>&& arg);
void f0(std::vector<int>&& v) {
static_assert(std::is_same&...
3
votes
1
answer
4k
views
What is the difference in atomic_load() and assignment?
I am working on a project that deals with lots of atomic operations. Till now I didn’t knew about atomic_load() and was only relying on assignment operator to get value of an atomic type and I haven’t ...
-1
votes
1
answer
352
views
How to use forwarding to cast rvalue to lvalue reference?
I'm trying to use an overloaded function to hide passing an int reference to a utility function when I don't care for the returned int data. Why is it that I need to use T& instead of T as the ...
3
votes
1
answer
99
views
why gcc 6.4.0 c++14 moves automatically lvalue to rvalue
I encountered a problem where gcc compiler moved local variable (not temporary) as rvalue argument to a function.
I have a simple example:
class A
{
public:
A() {}
A& operator=(const A&...
-1
votes
2
answers
823
views
error: invalid initialization of non-const reference of type 'std::function<void()>&' from an rvalue of type 'main()::<lambda()>'|
EDIT: Sorry, I asked this question without a thro understanding of references...
I seem to be getting this error when I run this code...
error: invalid initialization of non-const reference of type ...
-1
votes
1
answer
537
views
Why must a non-const reference be initialized with an lvalue?
Here is a snippet of code which causes a C2664 error:
cannot convert argument 1 from 'std::unique_ptr<Component,std::default_delete<_Ty>>' to 'ComPtr &'
So why must a non-const reference ...
2
votes
2
answers
233
views
Lvalue to rvalue conversion not performed
The following function returns an rvalue:
int foo()
{
int x = 42;
return x; // x is converted to prvalue
}
Clang's AST also shows the conversion:
`-FunctionDecl <line:1:1, line:5:1>...
3
votes
3
answers
175
views
Is the right operand of an assignment always converted to an rvalue?
I would like a clarification about this particular case:
class Test
{
Test& operator=(const Test& copy)
{
...
}
Test() = default;
}
Test a;
Test b;
b = a; //Is "a" ...
2
votes
1
answer
278
views
Lvalue to rvalue conversion with integer pointer
If I read implicit conversions correctly:
Lvalue to rvalue conversion
A glvalue of any non-function, non-array type T can be implicitly converted to a prvalue of the same type. [..]
Unless ...
0
votes
1
answer
275
views
Lvalue-to-rvalue conversion for class types: is there copying involved?
(I asked this question before but didn't give a viable example so I deleted previous one. I hope on this one I got the example right.)
Case:
#include <iostream>
struct S
{
S() = default;
...
3
votes
1
answer
274
views
Does a Comparison Between an Lvalue and a Literal Invoke an Lvalue-to-Rvalue Conversion?
I asked this question: static_assert of const Variable
And apparently it comes down to the question does a floating point lvalue get converted to an rvalue for the purposes of comparison?
So in this ...
8
votes
1
answer
202
views
What is the significance of special language in standard for lvalue-to-rvalue conversions for unsigned character types of indeterminate value
In the C++14 standard (n3797), the section on lvalue to rvalue conversions reads as follows (emphasis mine):
4.1 Lvalue-to-rvalue-conversion [conv.lval]
A glvalue (3.10) of a non-function, ...
13
votes
1
answer
1k
views
Why doesn't C++ move construct rvalue references by default? [duplicate]
Say I have the following function
void doWork(Widget && param) // param is an LVALUE of RRef type
{
Widget store = std::move(param);
}
Why do I need to cast param back to an rvalue ...
6
votes
2
answers
277
views
Can an lvalue at end of scope be treated as an rvalue?
EDIT: Consider 2 following examples:
std::string x;
{
std::string y = "extremely long text ...";
...
x = y; // *** (1)
}
do_something_with(x);
struct Y
{
Y();
Y(const Y&);
...
0
votes
1
answer
204
views
Lvalue-to-rvalue conversion in [expr.ref]/2
[expr.ref]/2:
For the first option (dot) the first expression shall be a glvalue
having complete class type. For the second option (arrow) the first
expression shall be a prvalue having pointer ...
0
votes
2
answers
686
views
Difference between std::forward implementation
Recently I've been trying to understand move semantics and came up with a question.
The question has already been discussed here.
I implemented the first variant and checked whether it returns l-...
4
votes
4
answers
2k
views
Why an Rvalue Reference is Turned into Lvalue Reference by a Universal Reference [duplicate]
I suppose when a universal reference parameter is matched with an rvalue reference argument, an rvalue reference argument is returned. However, my testing shows that the rvalue reference is turned ...
1
vote
1
answer
531
views
Is it an Rvalue or Lvalue After a Cast
The code here test for lvalue or rvalue after a type cast:
#include <stdio.h>
template <typename T>
T const f1(T const &t) {
printf("T const \n");
return t;
}
template <...
7
votes
2
answers
676
views
Assigning Rvalue returned from function to another Rvalue
class Test {
public:
int n1;
};
Test func() {
return Test();
}
int main() {
func() = Test();
}
This doesn't make sense to me. How and why is this allowed? Is it undefined ...
14
votes
2
answers
2k
views
Why is it possible to assign to an rvalue of class type?
Why is this code compiling? I thought that rvalues returned by the constructor are not located in memory and therefore can't be used as lvalues.
class Y {
public :
explicit Y(size_t num = 0) {}
};
...
0
votes
1
answer
878
views
Delete a node from binary search tree in C
I am trying to delete a node from a binary search tree. But when I want to test the function, I'll get an error message in 'case 3: two children'.
clist->name = temp->name;
This line causes ...
0
votes
0
answers
71
views
How std::move can work with copy-constructor that takes non-const reference? [duplicate]
I was reading about std::move. Based on quite few materials, I concluded that std::move is just a function that converts its argument type to the rvalue-reference.
I also read that, rvalue-references ...
0
votes
2
answers
126
views
How does rvalue reference work here?
I am puzzled by the following code:
#include <iostream>
int main()
{
int x{};
int&& rvx = static_cast<int&&>(x);
++rvx;
std::cout << x << std:...
4
votes
1
answer
1k
views
Understanding the example on lvalue-to-rvalue conversion
I have a hard time understanding how this code (an example from the C++14 draft standard [conv.lval]) invokes undefined behavior for g(false). Why does constexpr make the program valid?
Also, what ...
18
votes
3
answers
38k
views
Invalid initialization of non-const reference of type
In the following code, I'm not able to pass a temporary object as argument to the printAge function:
struct Person {
int age;
Person(int _age): age(_age) {}
};
void printAge(Person &person) {...