Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ &引用;“雷伊将军”;类在其析构函数中调用lambda_C++ - Fatal编程技术网

C++ &引用;“雷伊将军”;类在其析构函数中调用lambda

C++ &引用;“雷伊将军”;类在其析构函数中调用lambda,c++,C++,我编写了一个在其构造函数中调用lambda的类: template<typename Lambda> class scope_guard { public: scope_guard(const Lambda & f, bool e = true) : free(f), engaged(e) { } scope_guard(const scope_guard &) = delete; scope_guard & op

我编写了一个在其构造函数中调用lambda的类:

template<typename Lambda>
class scope_guard
{
public:

    scope_guard(const Lambda & f, bool e = true) : free(f), engaged(e)
    {
    }

    scope_guard(const scope_guard &) = delete;

    scope_guard & operator = (const scope_guard &) = delete;

    scope_guard(scope_guard && other) : free(std::move(other.free))
    {
        other.engaged = false;
    }

    scope_guard & operator = (scope_guard && other)
    {
        free = std::move(other.free);
        other.engaged = false;
    }

    ~scope_guard()
    {
        if (engaged)
        {
            free();
        }
    }

    void release()
    {
        engaged = false;
    }

private:

    Lambda free;

    bool engaged = true;
};

template <class Lambda>
inline scope_guard<Lambda> make_scope_guard(const Lambda & free, bool engaged = true)
{
    return scope_guard<Lambda>(free, engaged);
}


template <class Init, class Free>
inline scope_guard<Free> make_scope_guard(const Init & init, const Free & free, bool engaged = true)
{
    if (engaged)
    {
        init();
    }

    return make_scope_guard(free, engaged);
}

当然,我要问的不仅仅是OpenGL,而且一般来说,如果控件离开一个块时设置了一些东西并且应该取消设置,为什么不使用这个类呢?至少当我有这个通用机制并且不需要编写单独的类设置深度测试开/关或深度掩码开/关等时,它是有用的。。当然,这个类并不能替代现有的RAII类,如std::unique\u ptr。

乍一看似乎还可以,但您应该在d'tor中添加try..catch语句,以避免lambda引发任何意外的异常。我还将阻止赋值运算符和移动运算符,因为您通常不会复制此类对象,因为它们被限制在严格定义的范围内。如果析构函数中没有try/catch,会发生什么情况?这似乎非常类似。@Dmitriano,您正在执行自由代码,最好用try..catch语句包装这样的执行流,以避免意外的异常。如果您希望它成为项目中的一个通用模块,那么它应该易于使用,因此,您和其他程序员在使用它时应该具有最少的约束。一个这样的约束是将此类限制为非抛出Lambda。如果Lambda在应用程序中只使用了少量时间,那么Lambda方法是很好的。。。在像上面这样的情况下,如果我必须多次这样做,我可能会执行特定的类。乍一看,这似乎是可以的,但您肯定应该在您的任务中添加一个try..catch语句,以避免lambda抛出任何意外的异常。我还将阻止赋值运算符和移动运算符,因为您通常不会复制此类对象,因为它们被限制在严格定义的范围内。如果析构函数中没有try/catch,会发生什么情况?这似乎非常类似。@Dmitriano,您正在执行自由代码,最好用try..catch语句包装这样的执行流,以避免意外的异常。如果您希望它成为项目中的一个通用模块,那么它应该易于使用,因此,您和其他程序员在使用它时应该具有最少的约束。一个这样的约束是将此类限制为非抛出Lambda。如果Lambda在应用程序中只使用了少量时间,那么Lambda方法是很好的。。。在像上面这样的情况下,如果我必须做很多次,我可能会做一个特定的类。
{
    const bool enableDepthTest = IsDepthTestEnabledHere();
    auto guard = awl::make_scope_guard(
        []() { glEnable(GL_DEPTH_TEST); },
        []() { glDisable(GL_DEPTH_TEST); },
        enableDepthTest);

    //do OpenGL drawing here
}