如何在一对中存储(伴随)函子

How do I store (adjoint) functors in a pair?

本文关键字:伴随 函子 存储      更新时间:2023-10-16

我有两个函子是伴随的,即它们成对出现如果一个是doX(),那么另一个将是undoX()

他们是这样宣布的:

    template< typename T >
    struct doSomething{
        void operator()( T &x ) const {
        .....
        .....
        .....
        }
    };

    template< typename T >
    struct undoSomething{
        void operator()( T &x ) const {
        .....
        .....
        .....
        }
    };

这些由类在其成员变量上使用。

如何将它们存储在std::对中,以便在类的构造函数中传递?

附言:一个没有C++11或升压的解决方案将是可取的。但我愿意把它们作为最后的手段

容器类:

struct Container
{
   typedef int DoUndoType; // This is an example, the actual type will
                           // have to be decided by you.
   // The constructor and its argument.
   Container(std::pair<doSomething<DoUndoType>,
                       undoSomething<DoUndoType>> const& doUndoPair) : doUndoPair(doUndoPair) {}
   std::pair<doSomething<DoUndoType>,
             undoSomething<DoUndoType> doUndoPair;
};

集装箱类别的使用:

// Construct an object.
Container c(std::make_pair(doSomething<Container::DoUndoType>(),
                           unDoSOmething<Container::DoUndoType>()));
// Use the pair.
int arg = 10;
c.doUndoPair.first(arg);
c.doUndoPair.second(arg);