【问题标题】:Specify an inline callback function as an argument指定内联回调函数作为参数
【发布时间】:2010-05-08 23:18:12
【问题描述】:

让我先解释一下我想使用一些伪代码 (JavaScript) 来实现什么。

// Declare our function that takes a callback as as an argument, and calls the callback with true.
B(func) { func(true); }

// Call the function
B(function(bool success) { /* code that uses success */ });

我希望这说明了一切。如果没有,请对我的问题发表评论,以便我再写一点来澄清我的问题。

我想要的是在 C++ 中有这样的代码。

我曾尝试使用 lambda 函数,但无法为这些函数指定参数类型。

【问题讨论】:

    标签: c++ function callback inline


    【解决方案1】:

    如果您的编译器是相当新的版本(例如 Visual Studio 2010 或 GCC 4.5),您可以使用新 C++ 标准中的一些新功能,该标准目前正在批准中,应该很快就会发布。

    我不知道您需要做什么才能在 Visual Studio 中启用此功能,但它应该在 MSDN 或内部帮助中有详细记录。

    对于 GCC 4.5,只需添加 -std=c++0x 选项即可启用新功能。

    其中一个功能是Lambda syntax

    template <typename F>
    void func_with_callback(F f) {
        f(true);
    }
    
    int main() {
        func_with_callback( [](bool t){ if(t) cout << "lambda called" << endl; } );
    }
    

    如果您无法使用现代编译器,则可以使用仿函数和库(如 boost::lambda)等技术,它们的性能类似。

    【讨论】:

    • 这正是我想要的。我已经摆弄过模板和 lambda 表达式,但无法让它工作。附带说明一下,VS2010 让您无需额外更改即可使用它。
    • 我正在寻找保存函数参数(在本例中为f)以供以后使用,我该怎么做?
    • @Joel 与 std::function.
    【解决方案2】:

    编辑:再次阅读您的问题后,您似乎正在寻找 C++ 中的匿名函数。如果那是您想要的,不幸的是该语言不支持该功能。目前,C++ 要求您对这些事情更加冗长。如果您需要的不仅仅是boost::lamda 已经为您提供的功能,那么您可能应该将其作为正常功能分开。


    在 C 和 C++ 中,这是使用函数指针或仿函数和模板(仅限 C++)来完成的。

    例如(使用C++方式(函子))

    //Define a functor. A functor is nothing but a class which overloads
    //operator(). Inheriting from std::binary_function allows your functor
    //to operate cleanly with STL algorithms.
    struct MyFunctor : public std::binary_function<int, int, bool>
    {
        bool operator()(int a, int b) {
            return a < b;
        };
    };
    
    //Define a template which takes a functor type. Your functor should be
    //should be passed by value into the target function, and a functor should
    //not have internal state, making this copy cheap.
    template <typename Func_T>
    void MyFunctionUsingACallback(Func_T functor)
    {
        if (functor(a, b))
            //Do something
        else
            //Do something else
    }
    
    //Example usage.
    int main()
    {
        MyFunctionUsingACallback(MyFunctor());
    }
    

    使用C方式(函数指针):

    //Create a typedef for a function pointer type taking a pair of ints and
    //returning a boolean value.
    typedef bool (*Functor_T)(int, int);
    
    //An example callback function.
    bool MyFunctor(int a, int b)
    {
        return a < b;
    }
    
    //Note that you use the typedef'd function here.
    void MyFunctionUsingACallback(Functor_T functor)
    {
        if (functor(a, b))
            //Do something
        else
            //Do something else
    }
    
    //Example usage.
    int main()
    {
        MyFunctionUsingACallback(MyFunctor);
    }
    

    请注意,您应该更喜欢 C++ 方式,因为它允许编译器 在内联方面做出更明智的决定,除非出于某种原因 您仅限于 C 子集。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      相关资源
      最近更新 更多