【发布时间】:2011-05-27 16:53:18
【问题描述】:
我不确定要搜索什么,所以我会尽量解释清楚。在 STL 中,std::set 定义为
template <class Key, class Compare, class Allocator> class set;
Compare:比较类:接受两个与容器元素相同类型的参数并返回一个布尔值的类。表达式 comp(a,b),其中 comp 是此比较类的对象,a 和 b 是容器 [...] 的元素。这可以是实现函数调用运算符的类,也可以是指向函数[...]的指针。
我说的是Compare模板参数。
所以如果我要编写一个模板类,它有一个模板参数,它是一个实现函数调用运算符的类,我会写
template <class T, class Combine>
class MyClass
{
public:
Combine func;
MyClass()
{
func = Combine();
}
T do_it(T a, T b)
{
return func(a, b);
}
};
class IntCombine
{
public:
int operator () (int a, int b)
{
return a + b;
}
};
//...
MyClass<int, IntCombine> ob;
ob.do_it(4, 5);
或者,如果我要这样写第二个模板参数是一个函数:
template <class T, T Combine(T, T)>
class MyClass
{
public:
Combine func;
MyClass()
{
func = Combine;
}
T do_it(T a, T b)
{
return func(a, b);
}
};
int IntCombine(int a, int b)
{
return a + b;
}
//...
MyClass<int, IntCombine> ob;
ob.do_it(4, 5);
但是,在 STL 中,您可以使用任何一种方式使用 set 类。这是如何实现的?上面的代码只有在我的ob 定义中的第二个模板参数分别是实现operator () 的类或函数时才有效,但我不能编写MyClass 以便两者都可以工作。
我的示例可能看起来毫无用处。基本上我想写一个可以组合元素的容器,它和 STL 容器一样通用。
【问题讨论】:
标签: c++ templates operator-overloading function-pointers