从代码项目中获得了这个 ScopeExit 类,但它不会在 GCC 4.5.3 上构建。感谢任何帮助。
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 错误:
In member function ‘void test()’:
error: conversion from ‘test()::<lambda()>’ to non-scalar type ‘ScopeExit’ requested
编辑:
ScopeExit exit([&]() { }); // this works