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

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.

Related

How does std::bind and std::thread copy the arguments that are perfectly forwarded? [duplicate]

This question already has answers here:
move semantics/behaviors in std::bind and std::thread
(1 answer)
Where in the source does gcc's std::bind copy arguments into a data structure?
(2 answers)
Closed 3 months ago.
There is something I miss about std::bind and std::thread. Internally, they both forward the rvalue reference template argument (perfect forwarding). But it is known that it copies the object passed to it, instead we wrapp the lvalue references in std::ref(). If you pass a lvalue like the following:
template <typename T>
void fct(T&& x)
{
f(std::forward(x));
}
...
int r{9};
fct(r);
In this case, r is perfectly forwarded as an lvalue.
So, where does the copy occurs, since in the source code, they always use std::forward?
I understand the reason behind the copying step, but how is it really implemented?
Also, this could help me to understand the use of std::ref in depth.
I thank you in advance for your help.

How to resolve ambiguous function selection in C++? [duplicate]

This question already has answers here:
Overload resolution between object, rvalue reference, const reference
(1 answer)
C++ function overloading resolution involving pass-by-value, reference and constant reference
(3 answers)
Function Overloading Based on Value vs. Const Reference
(6 answers)
Closed 2 years ago.
How could i select the overloaded function that i mean to call ?
Consider this code:
void foo (std::vector<int> const &variable);
void foo (std::vector<int> variable);
For example in above code i want to call void foo (std::vector<int>);, I tried :
void bar ()
{
std::vector<int> tmp;
foo(tmp);
foo(static_cast<std::vector<int>(tmp));
foo(boost::implicit_cast<std::vector<int>>(tmp));
foo((std::vector<int>)tmp);
foo(std::vector<int>(tmp));
}
But it's failed, How could i do that without changing the functions signature ?

Why is this variable considered an lvalue? [duplicate]

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?

Difference or benefit of auto myFunc() -> int and int myFunc() [duplicate]

This question already has answers here:
Should the trailing return type syntax style become the default for new C++11 programs? [closed]
(4 answers)
Closed 8 years ago.
Having looked at a few online documents on C++14, I found the following syntax for defining a function in C++14 that uses trailing return types:
auto myFunc() -> int {}
my question is, other then using this way for using decltype in the argument and some other scenarios, is there a difference or any benefit for using the above syntax for bog standard functions like:
int myFunc() {}
Argument for : coherence.
This way you don't have the freak function needing a trailing return type standing out.
Argument against : wow that's ugly.[pers. opinion]
Semantic difference : none.

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.