- 2
C++ forward
--
25 Réponses
+ 3
For a template parameter T, && behaves differently than for a concrete type X. While X&& is a rvalue reference that can only be bound to movable objects, T&& is a forwarding reference (universal reference), which can be bound to mutable, immutable, and movable objects alike, and assumes their value category inside the function.
In the first case, you are calling the function with a string literal. Since pass-by-reference is used, the type will not decay, and be deduced to const char[ 4 ]. In order to call the print() function, it is therefore necessary to create a temporary string from the string literal. This leads to the rvalue overloaded print() to be chosen either way, hence the output.
In the second case, you are calling the function for a nonconstant object, and the universal reference is bound to a reference (std::string&). The function template std::forward<> forwards any nonconstant lvalue as a reference. Therefore, the lvalue overloaded print() is called in both instances.
+ 3
The differences between std::forward() and std::move() should have been explained sufficiently in existing sources, such as:
https://stackoverflow.com/questions/9671749/whats-the-difference-between-stdmove-and-stdforward
https://isocpp.org/blog/2018/02/quick-q-whats-the-difference-between-stdmove-and-stdforward
https://stackoverflow.com/questions/28828159/usage-of-stdforward-vs-stdmove
One can also refer to a reference:
https://en.cppreference.com/w/cpp/utility/forward
https://en.cppreference.com/w/cpp/utility/move
However, it would be interesting to know what you got from cxxdroid, and what you are referring to from cppreference being different than here.
+ 1
It is neither a compiler problem, nor is it a problem with std::forward() or anything. I already explained earlier why calling the function with a string literal will give an rvalue twice. Again, you are not calling it with a string rvalue, you are calling it with a string literal, which is a const char*, not a std::string instance. Your process of thought would only apply if you called the function for a movable std::string, e.g.
// explicit instantiation
c< std::string >( "foo" );
or
// convert literal to string
c( "foo"s );
The example you showed uses integers, where there is no issue between a literal and a variable.
+ 1
The value you moved from or moved to? Can you give an example? Unless you actually "steal" the resources from a movable object, e.g. via a move constructor or move assignment, its value will not change.
+ 1
No built-in type has a move constructor/ assignment operator, they are always copied. Moving them wouldn't make much sense.
0
Without forwarding in c function,it should always give lvalue
0
I tried cxxdroid and it gave different results
0
Also at cppreference it has different than here
0
Is it compiler problem
0
Also i dont understand what is the difference between move and forward
0
At cppreference it always give lvalue when passing parameters without forward
0
Either passing rvalue or lvalue
0
Here the link
0
If the use of forward is to accept lvalue and rvalue in templates
and also lvalue and rvalue are accepted without forward then forward is useless
0
Is it sololearn compiler problem
0
I think i got it
0
Also i have another question
0
When using std::move on function parameter the value i used to move to is still having the same value