Delegate in C++11

后端 未结 5 887
悲&欢浪女
悲&欢浪女 2021-02-14 00:09

Does C++11 provide delegates?

If not, what is the best (most efficient) way to do something similar in C++? Boost.Signals? FastDelegate? Something else?

5条回答
  •  时光取名叫无心
    2021-02-14 00:33

    You don't necessarily need to wait for C++0x. You can implement Delegates almost as well in the current C++03 standard. You simply have to overload the operator (), so as you can call MyObjectFunctor functor; functor(); and since the functor is an object, you can pass it as a delegate/object into functions;

    the current version of the STL defines the header that provides functions ready to be used with Functors/Lambdas/Delegates.

    simple example of a functor.

    struct FunctorDelegate
    {
        // as so to delegate as a function that takes an int input
        void operator()(int) 
        {
            // do what I want
        }
    };
    
    int main()
    {
        //... do some stuffs with an std::vector aRange; 
        FunctorDelegate functor;
        std::for_each(aRange.begin(), arange.end(), functor);
    }
    

提交回复
热议问题