如何在一对中存储(伴随)函子
How do I store (adjoint) functors in a pair?
我有两个函子是伴随的,即它们成对出现如果一个是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);
相关文章:
- () 函子后面的括号,而不是函数指针?
- 将方法转换为调用该方法的成员函子对象会导致崩溃
- C++函子,有什么有效的方法吗?
- 在 C++11 中获取函子作为参数
- 为什么我不能返回带有透明函子的法线映射引用?
- C++函子以防止重复?
- C++:对函子重载调用运算符的未定义引用
- 隐式转换为比较函数对象(函子)用于 std::sort 而不是 std::map?
- C++模板函数中,指定回调函子/lambda 的参数类型,同时仍允许内联?
- 函子还有更有用的用例吗?
- 完美转发C++重载和模板化函子及其参数
- 有没有办法从同一类中的函子调用类方法?
- std::bind() 参数列表中函子的执行顺序(可能与函数参数的求值顺序无关)
- 将函子传递到模板化函数中
- C++函子作为函数的输出参数
- 让线程拥有它C++运行的函子
- IDE 认为函子是构造函数?
- C++区分函子和值模板参数
- qsort 比较函子的分段错误
- 如何在一对中存储(伴随)函子