- 2
4th Apr 2021, 6:51 PM
return 0;
return 0; - avatar
25 odpowiedzi
+ 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.
4th Apr 2021, 9:53 PM
Shadow
Shadow - avatar
+ 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.
4th Apr 2021, 9:58 PM
Shadow
Shadow - avatar
+ 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.
5th Apr 2021, 8:14 AM
Shadow
Shadow - avatar
+ 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.
5th Apr 2021, 9:53 AM
Shadow
Shadow - avatar
+ 1
No built-in type has a move constructor/ assignment operator, they are always copied. Moving them wouldn't make much sense.
5th Apr 2021, 11:03 AM
Shadow
Shadow - avatar
0
Without forwarding in c function,it should always give lvalue
4th Apr 2021, 6:52 PM
return 0;
return 0; - avatar
0
I tried cxxdroid and it gave different results
4th Apr 2021, 6:52 PM
return 0;
return 0; - avatar
0
Also at cppreference it has different than here
4th Apr 2021, 6:53 PM
return 0;
return 0; - avatar
0
Is it compiler problem
4th Apr 2021, 6:53 PM
return 0;
return 0; - avatar
0
Also i dont understand what is the difference between move and forward
4th Apr 2021, 6:54 PM
return 0;
return 0; - avatar
0
At cppreference it always give lvalue when passing parameters without forward
5th Apr 2021, 7:40 AM
return 0;
return 0; - avatar
0
Either passing rvalue or lvalue
5th Apr 2021, 7:42 AM
return 0;
return 0; - avatar
0
Here the link
5th Apr 2021, 7:49 AM
return 0;
return 0; - avatar
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
5th Apr 2021, 7:51 AM
return 0;
return 0; - avatar
0
Is it sololearn compiler problem
5th Apr 2021, 7:51 AM
return 0;
return 0; - avatar
0
I think i got it
5th Apr 2021, 9:06 AM
return 0;
return 0; - avatar
0
Also i have another question
5th Apr 2021, 9:10 AM
return 0;
return 0; - avatar
0
When using std::move on function parameter the value i used to move to is still having the same value
5th Apr 2021, 9:10 AM
return 0;
return 0; - avatar