用什么宏可以用更短的东西替换“模板...”样板文件? 即:而不是这些:
template <typename NodeDataT, typename ArcDataT>
/*constructor*/ GraphDirected::
GraphDirected()
{
}
template <typename NodeDataT, typename ArcDataT>
/*destructor*/ GraphDirected::
~GraphDirected()
{
clear();
}
template <typename NodeDataT, typename ArcDataT>
void GraphDirected::
clear()
{
nodes.clear();
arcs.clear();
}
我想这样写:
boilerplate(/*constructor*/)
GraphDirected()
{
}
boilerplate(/*destructor*/)
~GraphDirected()
{
clear();
}
boilerplate(void)
clear()
{
nodes.clear();
arcs.clear();
}
当然,最后我需要一些保护(?#undef?),以免其他文件被弄乱。
如果不是更复杂,如何处理这些丑陋的东西?:
template <typename ElemType>
typename BST<ElemType>::nodeT * BST<ElemType>::
recFindNode(nodeT *t, ElemType & key) { ... }
最佳答案
我不会建议您仅仅为了避免出现一些标记而使用 MACRO。由于类模板的成员定义必须在同一个文件中,因此您必须在同一个文件中定义它们,即使您在类之外进行定义也是如此。
更好的解决方案是定义成员在类本身中,这样您就不必在每次定义成员时都重复标记。
请记住,模板会生成丑陋的错误消息,而 MACRO 是邪恶的。如果将两者结合使用,当出现问题时,您会看到更加丑陋和疯狂的错误消息。
关于c++ - 如何用宏替换 "template <typename ..."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14221011/