在C++范型编程中如何只特化类的一个成员函数(三)
= true };
};
template
class CEvent
{
public:
other function
....
return_type operator()(first_type p1, second_type p2)
{
if(IsVoid::value)
{
cout << "return type is void" << endl;
//...
//invoker(p1, p2);
}
else
{
cout << "return type is not void" << endl;
return_type ret = return_type();
//...
//ret = invoker(p1, p2);
return ret;
}
}
};
但是我们很快会发现这种情况下if语句被编译进去了, 所以return_type是void的情况下还是会编译失败。
我们要解决的问题就是如何把这个if语句变成函数重载,于是我们想到如下实现:
template
struct IsVoid
{
enum { value = false };
};
template<>
struct IsVoid
{
enum { value = true };
};
template
class Int2Type
{
enum {value = v };
};
template
class CEvent
{
public:
//other function
//....
return_type InvokerImpl(first_type p1, second_type p2, Int2Type)
{
cout << "return type is void" << endl;
//...
//invoker(p1, p2);
}www.2cto.com
return_type InvokerImpl(first_type p1, second_type p2, Int2Type)
{
cout << "return type is not void" << endl;
return_type ret = return_type();
//...
//ret = invoker(p1, p2);
return ret;
}
return_type operator()(first_type p1, second_type p2)
{
return InvokerImpl(p1, p2, Int2Type::value>());
}
};
上面的实现首先通过编译时类型识别,然后再把识别后相应的bool值转成不同类型, 最后再利用不同类型函数重载实现。
最后总结下,我们可以看到,从编译时到运行时,从面向对象到普通范型编程再到模板元编程,C++复杂得让人无语, 也强大得让人无语, 而且C++语言本身是在不断发展的(C++11), 同一问题在C++中往往有多种解决方案,这些解决方案有的简单,有的复杂,有的高效, 也有的低效, 而我们的目标就是利用C++这把利器寻找简单而高效的解决方案。