Got this ScopeExit class off code-project but it would not build on GCC 4.5.3. Appreciate any help.
class ScopeExit : private boost::noncopyable
{
typedef std::function<void()> func_t;
public:
ScopeExit(func_t&& f) : func(f) {}
~ScopeExit() { func(); }
private:
// no default ctor
ScopeExit();
// Prohibit construction from lvalues.
ScopeExit(func_t&);
// Prohibit new/delete.
void* operator new(size_t);
void* operator new[](size_t);
void operator delete(void *);
void operator delete[](void *);
const func_t func;
};
ScopeExit exit = [&]() { };
gcc 4.5.3 errors:
In member function ‘void test()’:
error: conversion from ‘test()::<lambda()>’ to non-scalar type ‘ScopeExit’ requested
Edit:
ScopeExit exit([&]() { }); // this works
It’s copy/move initialization. Your copy c-tor is deleted, move c-tor is deleted too.
n3337 12.8/9
Have no ideas why first case doesn’t work, but this case works fine
EDIT.
When we use
copy-initializationof variable of class-type only standard and elipsis implicit conversions are used. Conversion from lambda to function pointer or from function pointer tostd::functionisuser-defined conversionand not used.n3337 8.5/16
n3337 13.3.1.4/1
n3337 13.3.2
n3337 13.3.3.1/4