A question turned up when debugging some code at work for race conditions: here is a reduced example:
//! Schedules a callable to be executed asynchronously
template<class F> void schedule(F &&f);
int main(void)
{
bool flag(false);
// Ignore the fact this is thread unsafe :)
schedule([&] { flag=true; });
// Can the compiler assume under strict aliasing that this check
// for flag being false can be eliminated?
if(!flag)
{
// do something
}
return 0;
}
Obviously the code fragment is thread unsafe - that bool flag needs to be a std::atomic and then the seq_cst memory ordering would force the compiler to always check the value being tested by if. This question isn't about that - it's about whether initialising a capture-all reference lambda tells the compiler that flag may have been aliased, and therefore to not constexpr elide the check for flag's value later on under optimisation?
My own personal guess is that constructing a [&flag] {...} lambda would suggest potential aliasing of flag, while a [&] {...} clobbering all auto initialised variables with being potentially aliased sounds too extreme an anti-optimisation so I'm guessing no to that. However, I would not be surprised if reference capturing lambdas don't alias clobber anything at all.
Over to you C++ language experts! And my thanks in advance.
Edit: I knew that the lack of thread safety would be seen as an answer, however that is not what I am asking. Let me reduce my example still further:
int main(void)
{
bool flag(false);
// Note that this is not invoked, just constructed.
auto foo=[&] { flag=true; };
// Can the compiler assume under strict aliasing that this check
// for flag being false can be eliminated?
if(!flag)
{
// do something
}
return 0;
}
Now can that check for flag being false be elided?
Edit: For those of you coming here in the future, my best understanding of the answers below is "yes, the check can be elided" i.e. constructing a lambda which takes a local variable by reference is not considered by the compiler's optimiser as potentially modifying that variable, and therefore the compiler's optimiser could legally elide subsequent reloads of that variable's storage. Thanks to everyone for your answers.
[&]doesn't "capture all references". It's merely syntactic sugar for "capture everything that is used by reference".