« first day (1013 days earlier)      last day (1842 days later) » 

6:28 AM
both g++ & MSVC do not have the overload for std::basic_string_view<char32_t>
what's wrong?
Oh, the stream also needs to be of char32_t
 
 
9 hours later…
3:43 PM
i had posted one query now. can someone please help me out from this problem?
 
 
6 hours later…
9:18 PM
Hi, I'm looking for help understanding rvalue references
I mean, I get the idea, but some things still are unclear, like should I overload all of my functions that take const& parameters to && parameters ?
 
nwp
No. You only do that when you want to move the parameter into another persistent variable.
And even then just taking by value removes the need to duplicate functions, at the cost of occasionally using an extra move.
 
I don't get it, why wouldn't I want to take an rvalue reference when I can take a const reference ? there are only advantages to be able to take rvalue references
 
nwp
No, there are disadvantages too. If you take an rvalue reference that means you want a temporary. That means the caller must provide a temporary which they otherwise would not.
For example void print(const std::string &s); is totally fine. Taking a std::string && instead means the function is less useful because it can only print temporaries and it doesn't even benefit from the fact that s is a temporary.
 
Yeah, but let's take an example. I have two instances of a custom type T names number1 and number2. The two following calls are equally likely, right ? funct(number1) or funct(number1 - number2)
but the first one gives T const& and the other one T&& right ?
 
nwp
Depends on what types are involved. number1 is probably a T & and number1 - number2 is probably a T.
But that doesn't mean you have to specialize for T &&. If funct does not move the T away then there is no point.
 
9:25 PM
isn't number1 - number2 a T&& ???? it's an rvalue, right ?
 
nwp
It depends on how operator-(const T&, const T&) is defined. Normally it will return a T.
And you seem to be confusing rvalue and temporary.
 
I probably am
The convention is to do a copy and return a T for operators I guess ?
 
nwp
Not for all operators.
You would expect that a - b returns a new object of the type of a and b and not change them.
You need to know what a temporary is, why that matters and how to turn a non-temporary into a temporary.
You want to know if something is a temporary because then you can do resource stealing.
For example an std::vector is essentially a pointer to some heap-allocated memory. If you make a copy then you have to allocate and move a bunch of objects.
But if you know that the vector is a temporary you can simply steal its pointer and save yourself a lot of trouble.
So we need to keep track of what is a temporary and what isn't. Also we need to be able to specify that we don't care about a value anymore and allow it being treated as a temporary.
The rule of thumb is that if you can take the address of the thing or it has a name then it is not a temporary.
So for example T &&t, the t is not a temporary because it has a name, t, and I can take the address of it with &t.
If I want to treat it as a temporary I use std::move(t) which allows whoever I give that to to steal resources from t because t dies anyways.
 
@nwp I get it. I'm reading stackoverflow.com/questions/36232671 in parallel to understand it well
so how can I turn a T temporary into an rvalue reference ?
 
nwp
You use std::move on it.
std::move(t) returns a T && and allows resource stealing.
Which the other side may or may not do.
 
9:35 PM
so automatic cast from temporaries to rvalue references doesn't exist the way automatic cast from value to value reference exist ?
 
nwp
There is no such thing as cast from temporary to rvalue reference. That would be pointless.
Temporaries already prefer T && over const T&.
 
yeah so we can consider it as an automatic, implicit cast then I guess
 
nwp
In other words, std::string{"Hello"} is a temporary. It has no name and you cannot take the address of it and if you pass it to an overloaded function taking a const std::string & and another taking a std::string && then the second will be chosen.
"Implicit cast" doesn't exist in C++. Casts are always explicit. The other thing is an implicit conversion. Although with references people usually say binding instead of converting.
 
I understand. Is val2 - val1 a temporary ? when we have T operator-(T const& lhs, T const& rhs) ?
 
nwp
Yes. It has no name and you cannot take the address of it.
 
9:42 PM
So, when it's useful, I should always overload T funct(T const&) with T funct(T&&) in order to take advantage of the fact that temporaries cast to T&& which give me more room for optimization ?
 
nwp
If you return the value you are given then yes, that would sometimes mean you can skip a copy.
Another way is to write T funct(T). Either the copy constructor or the move constructor will be invoked to create that parameter which includes the copy skip and the you simply move the parameter.
Although this is a special case.
When you have a local variable, including a parameter, you don't explicitly use std::move.
C++ is usually stupid when it comes to detecting temporaries, but for local variables it has a special rule.
 
Is that whole thing related to perfect forwarding somehow ?
 
nwp
It is. When you have template <class T> and take a T && then this can bind both to a temporary and to a non-temporary.
This is bad, because you don't know if you can steal resources or not.
The solution is to use std::forward<T>(t) which will cast to T && if a temporary was bound to it and to T & or const T & if it wasn't.
 
I think I understand. Using std::forward helps calling the right operator/constructor/etc ?
Do you have any good readings to help me deepen my - still weak I believe - understanding of the domain ?
 
nwp
It casts to the right reference type which then causes overload resolution to pick the right function.
 
9:55 PM
Why can't it be made implicit, like temporaries binding to rvalue references ?
 
nwp
People like to say "std::move doesn't move and std::forward doesn't forward". Technically they only cast to the right reference type to help overload resolution.
2
 
(sorry for all the questions I really hate not understanding x) )
 
nwp
That is already the case. Temporaries bind to rvalue references. That's the whole point of having rvalue references.
 
okay
thanks for your help !
 
nwp
I don't really have any resources besides searching for "move semantics" which you probably already did.
 

« first day (1013 days earlier)      last day (1842 days later) »