简体   繁体   English

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

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

I have two functors which are adjoint ie they occur in pairs If one is doX() , the other will be undoX() .我有两个伴随的函子,即它们成对出现如果一个是doX() ,另一个将是undoX()

They have been declared like so:他们是这样声明的:

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

        .....
        .....
        .....

        }
    };


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

        .....
        .....
        .....

        }
    };

These are used by a class on it's member variables.这些由类在其成员变量上使用。

How can I store them in an std::pair which I can pass in the class's constructor?我如何将它们存储在一个 std::pair 中,我可以在类的构造函数中传递它?

PS A solution without C++11 or boost would be appreciated. PS 没有 C++11 或 boost 的解决方案将不胜感激。 But I'm willing to use them as a last resort但我愿意用它们作为最后的手段

Container class:容器类:

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;
};

Use of Container class: Container类的使用:

// 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM