Why is this variable considered an lvalue? [duplicate] - c++

This question already has answers here:
Why are rvalues references variables not rvalue?
(3 answers)
Rvalue Reference is Treated as an Lvalue?
(4 answers)
Closed 3 years ago.
Considering this code
class T {
public:
T(T& x) = delete;
T(T&& x) {}
};
void foo(T&& b) {
T y(b);
}
I was expecting that b; which is an rvalue by declaration; and seemingly usage, should be passed into the move constructor of T in foo().
Instead; I get a compilation error reporting that T& has been deleted.
Replacing it with
void foo(T&& c) {
T y(std::move(c));
}
Results in the expected success; but obviously one doesn't want to litter their code with std::move everywhere.
As tempting as it is to blame visual studio - in this case I suspect it's my understanding that's wrong. Can someone please explain why move constructor isn't used?

Related

What is the fundamental difference between T&& and int&&? [duplicate]

This question already has answers here:
Is there a difference between universal references and forwarding references?
(2 answers)
Rvalue reference parameters and template functions
(2 answers)
Closed 2 years ago.
Can anyone please explain me the difference between this 2 functions?
template<typename T>
void f(T&& a) {
}
void f(int&& a) {
}
int main(){
int a = 3;
f(a);
}
If I only have the template version, the code compiles and works fine.
But if I remove the template and leave the int&& version, the compiler complains and I have to
transform the call into f(std::move(a)) in order to make it work.
Can anyone please explain me the difference here?
Thanks in advance.

Reference as last symbol in C++ function declaration? [duplicate]

This question already has answers here:
What does the single ampersand after the parameter list of a member function declaration mean?
(3 answers)
What is "rvalue reference for *this"?
(3 answers)
Closed 6 years ago.
Looking at Boost::Optional optional class template header I come across this:
T const& operator*() const&
T& operator*() &;
T&& operator*() &&;
For the life of me I can't find this syntax anywhere else (a reference as the last symbol) I would assume it has something to do with overloading on the type(const l-val, l-val, r-val) of the object the operator belongs to, but I haven't seen this described anywhere.
Could someone tell me what this syntax means?

C++ access rvalue referenced object from non rvalue object [duplicate]

This question already has answers here:
My attempt at value initialization is interpreted as a function declaration, and why doesn't A a(()); solve it?
(5 answers)
Closed 8 years ago.
This question may be sound stupid. I just want to make sure. And maybe that someone point me where this described in standard.
We cannot have rvalue referenced objects inside lvalue. Right?
struct A{int value;};
struct B{
B(A &&value) : a(std::forward<A>(value)){}
A&& a;
};
int main()
{
// allowed
B(A()).a;
// error
B b(A());
b.a;
return 0;
}
http://coliru.stacked-crooked.com/a/ea6bd617d421a8b8
B b(A()); declares a function (most vexing parse). You get a compiler error because b has no member a.
To fix the issue write B b{A()} instead.

Prevent assigning to rvalue [duplicate]

This question already has an answer here:
Operator overload which permits capturing with rvalue but not assigning to
(1 answer)
Closed 8 years ago.
Say I have a class with an overridden assignment operator:
class Test
{
public:
Test& operator =(const Test&) {return *this;}
};
Test f();
Is there any way to make it a compile-time error to assign to the result of a function?
Examples:
f() = test();
Test t;
f() = t;
// if there were also a Test Test::operator+(Test);
(t + t) = test();
This already happens for primitive types, and I want to replicate it for this class:
int g();
g() = 5; // error
(3 + 4) = 5; // error
Edit: I'm using Visual C++, which doesn't support all of C++11. Specifically, it doesn't support ref-qualifiers.
If you don't want your operator=() to be invoked on temporaries (rvalues), then qualify it lvalue as:
Test& operator =(const Test&) & {return *this;}
//^^^ note this
Now the following
f() = t;
will give compilation error.
In C++11, you can ref-qualify your assignment operator so that it can only be called on lvalue references. However, I can't find anything definite in the standard about whether such an operator is still the copy assignment operator.
In C++03, people came up with the idea of only returning const objects from functions, but that's a bad idea because it prevents moving in C++11.
In the end, it's probably not worth going to any effort to do this.

VS2010: Temporaries can't be bound to non-const references [duplicate]

This question already has answers here:
rvalue to lvalue conversion Visual Studio
(3 answers)
Closed 8 years ago.
I came to know that Temporaries connot be bound to non-const references.
class X
{
int i;
};
X fun()
{
return X();
}
void func(X &x)
{
}
int main()
{
func(fun());
return 0;
}
Isn't call to fun producing a temporary? Why can temporary be linked to non-const reference here. I am unable to comprehend as to why is this compiling fine.
EDIT:
I am using VS2010. I don't understand how should this matter.
Isn't call to fun producing a temporary?
Yes.
Why can temporary be linked to non-const reference here.
It can't.
I am unable to comprehend as to why is this compiling fine.
Because your compiler is faulty.
I am using VS2010. I don't understand how should this matter.
That compiler has many non-standard "extensions" to the language. This is just one example of dodgy code that's accepted by that compiler, but not a conformant one.