Given this code:
template <class Func> class ScopeGuard
{
public:
/** @param func function object to be executed in dtor
*/
explicit ScopeGuard( Func && func ) : m_func( std::move(func) ) {}
~ScopeGuard()
{
if (m_bDismissed)
return;
m_func();
}
/** Dismisses the scope guard, i.e. the function won't
be executed.
*/
void dismiss() { m_bDismissed = true; }
private:
// noncopyable until we have good reasons...
ScopeGuard(const ScopeGuard&) = delete;
ScopeGuard& operator=(const ScopeGuard&) = delete;
Func m_func;
bool m_bDismissed = false;
};
// Get functor for cleanup to use in FlagRestorationGuard
auto GetFlagRestorationGuard(bool& i_flagRef)
{
return [&i_flagRef, resetVal = i_flagRef] { i_flagRef = resetVal; };
}
class FlagRestorationGuard : public ScopeGuard<decltype(GetFlagRestorationGuard(*(new bool)))>
{
public:
FlagRestorationGuard( bool& i_flagRef, bool i_temporaryValue )
: ScopeGuard(GetFlagRestorationGuard(i_flagRef))
{
i_flagRef = i_temporaryValue;
}
};
I get the following error building with Apple Clang for GetFlagRestorationGuard(*(new bool))
:
error: expression with side effects has no effect in an unevaluated context [-Werror,-Wunevaluated-expression]
Note that this code builds and works fine with MSVC 2017.
Of course, it's possible to re-write it all to use a struct with operator()()
instead of lambda and a function returning it, but I wonder if there is a nice way to use lambda like this?
Reference for the actual code:
Reference to build failure: