我有一个名为类的ValueChecker'将派生类型传递给基础成员模板函数的优雅方式?
其具有以下成员函数:
template<typename T>
bool ValueChecker::checkMe(std::ostringstream &oss, T &me) {
std::cout << "Default checkMe() for " << typeid(me).name() << std::endl;
return true;
}
类ValueChecker意图做派生类的值有一些简单的检查。 checkMe()最终会得到专门为不同的派生类:
class Airplane : public ValueChecker {
friend class ValueChecker;
[...]
}
template<>
bool ValueChecker::checkMe<Airplane>(std::ostringstream &oss, Airplane &me) {
...
/* Actually, this code is generated from a simple file which translates
* a simple language into C++ code. So that a non-developer can write
* the simple checks.
*
* ValueChecker itself has utility functions that may be called in the
* template specialization which are shared across all types.
*/
}
这工作,但只是一个小问题,checkMe的声明,当你在调用:
int main() {
Airplane plane;
std::ostringstream oss;
if(plane.checkMe(oss, plane)) {
cout << "Values are bogus! " << oss.str() << endl;
return 0;
}
我调用plane.checkMe(oss,plane)。但是,我可以通过另一架飞机,而不是检查飞机。此外,调用是多余的?意思是,理论上,编译器应该根据平面的类型知道调用哪个模板函数。不应该有必要将它作为一个参数来传递?无论如何,最好不要消除最后一个论点。所以这样的电话会很好:
if(plane.checkMe(oss)) { ... } // Calls the right template specialization.
我只是不能得到它的工作。 C++专家在这里能帮助我吗?谢谢。
我们不能在typeid中传递这个指针吗? –
你评价过CRTP吗?(http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern) – Chubsdad
这首先看起来很奇怪。为什么'Airplane'派生自'ValueChecker'?为什么'checkMe()'当(从你的问题来判断)它应该只检查'this'的时候带一个'T'类型的参数? – Angew