Shayan RC
Shayan RC

Reputation: 3322

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

I have two functors which are adjoint i.e. they occur in pairs If one is doX() , the other will be 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?

P.S. A solution without C++11 or boost would be appreciated. But I'm willing to use them as a last resort

Upvotes: 2

Views: 83

Answers (1)

R Sahu
R Sahu

Reputation: 206577

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:

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

Upvotes: 2

Related Questions