« first day (1561 days earlier)      last day (1295 days later) » 

2:43 AM
@milleniumbug OK thanks :p
 
 
8 hours later…
11:06 AM
@kenkar so remember in C++ that optimization is allowed regardless of anything else via the AS-IF principle. So if something is never observed it can be removed. You're never reading your vect so likely the compiler is just removing it
 
 
1 hour later…
Ron
12:28 PM
Does declaring a thread starts one? As in std::thread t;?
 
Ron
Is there a way to explicitly start a declared thread later on?
My bad, that is covered. What is the practical use of a threads .swap()?
 
1:24 PM
@Ron thread member variables come to mind
 
Ron
1:47 PM
Can a thread object call a different function overload based on an argument?
How to choose a proper overload resolution based on a std::thread(overloadedfn, arg)?
 
jrh
2:32 PM
SomeStruct& ReturnsValueRef()
{
	// some code to return a ref to a SomeStruct
}

SomeStruct GetStruct(void)
{
	// store a reference to some value, sure, makes sense
	SomeStruct& value = ReturnsValueRef();

	// and then... return it by value? (Implied copy?)
	return value;
}

int main()
{
	// Since GetStruct returns by value, aren't we just doing another implied copy? I can't think of a reason to store it as a ref.
	SomeStruct& value = GetStruct();
}
Is my analysis of this right? I can't think of a reason to store a return value of a function (that returns by value) as a ref, it seems a bit off, I'm thinking it's a mistake.
 
@jrh it is, returning by ref if the item is local to the method is undefined behavior in most cases
 
jrh
ReturnsValueRef() is not returning a local to the method
 
also taking the ref of a temp is undefined behavior
 
jrh
@Mgetz the temp being in SomeStruct& value = GetStruct(); ?
 
@jrh yes
 
jrh
2:35 PM
Thanks, I was pretty sure it was UB
 
either way be careful about that, there are valid reasons to do it. But be careful with it because it's almost always UB
not always... but usually
 
jrh
Yeah I wouldn't write stuff like this
I'm trying to unscramble some library code
This is so far confirming my theory that they didn't really go into their memory management with a clear strategy
this smells like a bug report, I think
Is that an upside of having no documentation on your library? Free UB audits because everyone has to read the source? Hmm...
 
@jrh depends? it is valid in some cases to return a reference as something internal
 
jrh
@Mgetz do you have a reference on what would make it UB?
 
@jrh lifetime
 
jrh
2:47 PM
I guess what I'm stuck on is, a function GetStruct() that returns by value (e.g., returns a non-const int local variable), but is stored by reference. Does the fact that GetStruct() returns by value make the lifetime of that return value the lifetime of value in main()or does the original lifetime in GetStruct() still apply?
 
@jrh well that's UB
because it's a non-const ref
 
jrh
So the temporary storing the return value of GetStruct() must be const?
because if I dropped the & from SomeStruct& value = GetStruct(); and made it SomeStruct value = GetStruct(); it'd be valid
I may not be imagining this right though, I figured that it'd make a temporary non-const local variable to store the return value, then it takes a reference to it
 
jrh
3:09 PM
So SomeStruct& value = GetStruct();is an "expression" and the temporary that the return value of GetStruct() is created in, is destroyed as soon as that line ends, in C++11?
er wait ref extension
> a temporary bound to a return value of a function in a return statement is not extended: it is destroyed immediately at the end of the return expression. Such function always returns a dangling reference.
 
@jrh unless the ref is const
yeah... you can only extend the lifetime of a temporary using a const ref
 
jrh
I see no mention of const refs here en.cppreference.com/w/cpp/language/…
It'd appear that it'd fall under "a temporary bound to a return value of a function in a return statement is not extended"
 
@jrh a non-const reference would extend the lifetime per the rules. But binding the temporary to a non-const reference is not allowed, so it's not worth mentioning there
also I think msvc has the binding to a non-const reference as a compiler extension, so make sure to turn those off if not needed
 
@jrh trying to find it explicitly, but AFAIK binding a temp to a const ref is valid
@PeterT pretty sure you have to explicitly disable this warning as an error now. Otherwise they kill compile. IIRC it's still technically UB but they allow it in some circumstances for legacy code if you disable the warning but it's an ABI artifact not intended.
 
jrh
@PeterT How would you extend the lifetime of the return value except via binding a temporary to a reference? Or is there no way, and that's your point, that it's always invalid?
 
3:23 PM
yes that was my point, it's not part of the standard.
 
jrh
Okay, thanks!
 
but the wiki seems (probably with good intent) to make so that's not the case
 
jrh
Ok, so a specific exception just for that, I see; wish they moved that exception up a bit
 
@jrh it's not wise to use the exception, I'm not even sure why it exists
I think it's around to allow people to return things like std::vector temporarily without making a copy back before move semantics was a thing
but honestly? value semantics and move probably completely obsolete it
 
jrh
Yeah I wouldn't recommend people store return by value as ref, I'd just store the value and hope the compiler optimized it out
 
3:28 PM
it's required to
so again... legacy code only
my guess is that it was for something like this:
 const std::vector<int>& tempRef = foo();
 std::vector<int> bar;
 std::swap(tempRef, bar);
but I could be wrong
 
jrh
hm, I see. Thanks again!
So if it is a const ref, the exception for const ref takes precedence over the rule that says it's not an exception, right?
The note says Note that rvalue references and lvalue references to const extend the lifetimes of temporary objects then says "Check out the rules and exceptions"
This is getting a bit silly, surely it takes precedence right? otherwise that note would be meaningless
So you've got the note saying "Note that rvalue references and lvalue references to const extend the lifetimes of temporary objects, see exceptions" with that link saying, as an exception a temporary bound to a return value of a function in a return statement is not extended: it is destroyed immediately at the end of the return expression. Such function always returns a dangling reference.
So is it saying rvalue references extend for const refs, but not for return values? So it's still UB?
 
@jrh honestly? I'd just ignore that and treat is as UB unless you really have a good reason not to. There are corner cases with things like iterators... but those have known lifetime semantics
 

« first day (1561 days earlier)      last day (1295 days later) »