C++ 如何成对存储(伴随)函子?
我有两个伴随的函子,它们成对出现 如果一个是C++ 如何成对存储(伴随)函子?,c++,functor,C++,Functor,我有两个伴随的函子,它们成对出现 如果一个是doX(),另一个将是undoX() 已宣布如下: template< typename T > struct doSomething{ void operator()( T &x ) const { ..... ..... ..... } }; template< typename T > s
doX()
,另一个将是undoX()
已宣布如下:
template< typename T >
struct doSomething{
void operator()( T &x ) const {
.....
.....
.....
}
};
template< typename T >
struct undoSomething{
void operator()( T &x ) const {
.....
.....
.....
}
};
模板
结构剂量测量{
void运算符()(T&x)常量{
.....
.....
.....
}
};
模板
结构撤销某物{
void运算符()(T&x)常量{
.....
.....
.....
}
};
类在其成员变量上使用这些变量
如何将它们存储在std::pair中,并将其传递到类的构造函数中
另外,不使用C++11或boost的解决方案将不胜感激。但我愿意把它们作为最后的手段集装箱类:
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);
struct容器
{
typedef int DoUndoType;//这是一个示例,实际类型将
//必须由你决定。
//构造函数及其参数。
容器(std::pair const&doUndoPair):doUndoPair(doUndoPair){
std::pairHow我应该在这里使用函子吗?我应该使用c.first和c.second吗?@ShayanRC我扩展了我的答案来回答您的问题。@ShayanRC您已经定义了操作符()
函数获取一个T&
。现在我考虑一下,除非它被更改为T const&
,否则我编写的代码不会编译。@ShayanRC修复了答案。@ShayanRC 10只是一个示例值。您必须选择一个更适合您正在执行的操作的值。