Can Lambda expression be downgraded to C++ 98

前端 未结 2 1928
天涯浪人
天涯浪人 2021-01-21 01:47

I recently got a problem need to integrate C++ 11 code written with lambda expression to old code base which only support C++ 98 compiler. I figured out couple of possible equiv

2条回答
  •  后悔当初
    2021-01-21 01:49

    With Functor:

    struct Functor
    {
        explicit Functor(uint attributes) : attributes(attributes) {}
        void operator () (uint index, void * objectAddress) const
        {
            if ((ObjectInfo(index) & attributes) != 0)
            {
                fn(index, objectAddress);
            }
        }
        uint attributes;
    };
    

    And then call

    uint attributes = 0x0030;
    // ....
    ForEachObject(Functor(attributes));
    

    For each different lambda, you have to write a functor. You don't have to modify ForEachObject

提交回复
热议问题