为什么不能在模板函数中使用 NULL 作为默认指针参数? 让我们考虑以下代码:
template<class Graph, class NodeAttribs, class ArcAttribs> string
graphToGraphviz(Graph &graph,
NodeAttribs *nattribs = NULL,
ArcAttribs *aattribs = NULL,
string name = ""){
/*...*/
}
我希望能够这样调用它:
graphToGraphviz(g);
我怀疑编译器认为它无法解析 NULL 的类型,但如果属性为 NULL (有 if 条件),则不会使用这些类型。但编译器可能无法以正确的方式解决这种情况。如果是,我怎样才能编写这样的重载函数,这将允许我使用简短的形式?
我有一个像这样重载它的想法:
class Empty{}
template<class Graph> string
graphToGraphViz(Graph &graph,
string name = ""){
return graphToGraphviz<Graph, Empty, Empty>(graph, NULL, NULL, name)
}
但是编译器给了我错误,其中包括类 Empty 没有定义 operator []
。这又不稳定,但是我是否必须制作所有这些“虚拟”运算符重载和空函数才能满足编译器的要求,或者是否有更好的方法来做到这一点?
编辑: 请查看完整的源代码 - 它将 Lemon graph 转换为 graphviz 格式: 我尝试使用 C++11 中的新语法(如下面的答案所示),但没有成功。
#ifndef GRAPHTOGRAPHVIZ_H_
#define GRAPHTOGRAPHVIZ_H_
#include <lemon/list_graph.h>
using namespace lemon;
using namespace std;
/* USAGE:
* ListDigraph::NodeMap<unordered_map<string, string>> nodeAttribs(g);
* ListDigraph::ArcMap<unordered_map<string, string>> arcAttribs(g);
* nodeAttribs[node]["label"] = "node_label";
* string dot = graphToGraphviz(g, &nodeAttribs, &arcAttribs, "hello");
*/
template<class Map>
string getAttribs(Map &map){
string attribs = "";
for (const auto &el : map){
if (el.second != "")
attribs += "\"" + el.first + "\"=\"" + el.second + "\",";
}
if (attribs != "")
attribs = " [" + attribs + "]";
return attribs;
}
template<class Graph, class NodeAttribs, class ArcAttribs> string
graphToGraphviz(Graph &graph,
NodeAttribs *nattribs = NULL,
ArcAttribs *aattribs = NULL,
string name = ""){
typedef typename Graph::template NodeMap<string> NodeMap;
typedef typename Graph::NodeIt NodeIterator;
typedef typename Graph::ArcIt ArcIterator;
NodeMap labels(graph);
ostringstream layout;
layout << "strict digraph \""+name+"\" {\n";
// prepare labels
for (NodeIterator node(graph); node != INVALID; ++node){
string label = "";
if (*nattribs != NULL)
label = (*nattribs)[node]["label"];
if (label == "") label = static_cast<ostringstream*>( &(ostringstream() << graph.id(node)) )->str();
label = "\"" + label + "\"";
labels[node] = label;
}
// initialize nodes
for (NodeIterator node(graph); node != INVALID; ++node){
layout << labels[node];
if (*nattribs != NULL)
layout << getAttribs((*nattribs)[node]);
layout << ";" << std::endl;
}
// initialize arcs
for (ArcIterator arc(graph); arc != INVALID; ++arc){
layout << labels[graph.source(arc)] << "->" << labels[graph.target(arc)];
if (*aattribs != NULL)
layout << getAttribs((*aattribs)[arc]);
layout << ";" << std::endl;
}
layout << "}";
return layout.str();
}
#endif /* GRAPHTOGRAPHVIZ_H_ */
使用 C++11 语法,函数头将如下所示:
template<class Graph, class NodeAttribs=ListDigraph::NodeMap<string>, class ArcAttribs=ListDigraph::NodeMap<string> > string
graphToGraphviz(Graph &graph,
NodeAttribs *nattribs = NULL,
ArcAttribs *aattribs = NULL,
string name = "")
但它无法编译并给出大量奇怪的错误。
最佳答案
编译器在调用时出现问题:
graphToGraphviz(g);
现在 NodeAttribs
和 ArcAttribs
的类型是什么?
无论您是否使用它,编译器都必须推断出它的类型。因为使用或不使用是运行时检查。
使用您当前的代码,上述类型将变得不可推导。
how could I write such overloaded function
你的问题有答案了!!
重载模板
函数,从原始模板函数中删除默认参数,并让两个函数共存:
template<class Graph>
string graphToGraphviz(Graph &graph, string name = "");
关于c++ - Null 作为默认模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13525127/