2

为什么不能在模板函数中使用 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 []定义。这又是不稳定的,但是我是否必须使所有这些“虚拟”运算符重载和空函数来满足编译器的要求,还是有更好的方法来做到这一点?

编辑:请查看完整的源代码 - 它将柠檬图转换为 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      = "")

但它不会编译并给出大量奇怪的错误。

4

3 回答 3

2

调用时编译器出现问题:

graphToGraphviz(g);

现在NodeAttribsand的类型是ArcAttribs什么?
无论您是否使用它,编译器都必须推断出它的类型。因为使用或不使用是运行时检查。
使用您当前的代码,上述类型变为non-deducible

我怎么能写出这样的重载函数

你的问题有答案!!
重载函数,从原始模板函数中template删除默认参数 ,让两个函数共存:

template<class Graph>
string graphToGraphviz(Graph &graph, string name = "");
于 2012-11-23T08:15:04.737 回答
1

你有没有尝试过

template<class Graph, class NodeAttribs, class ArcAttribs> string
graphToGraphviz(Graph       &graph,
                NodeAttribs *nattribs = (<NodeAttribsClass>*)NULL,
                ArcAttribs  *aattribs = (<ArcAttribsClass>*)NULL,
                string      name      = ""){
   /*...*/
}

或者

template<class Graph, class NodeAttribs = NodeAttribsClass, class ArcAttribs = ArcAttribsClass> string
graphToGraphviz(Graph       &graph,
                NodeAttribs *nattribs = NULL,
                ArcAttribs  *aattribs = NULL,
                string      name      = ""){
   /*...*/
}

可以在该插槽中使用的有效(具体)类在NodeAttribsClass哪里 ?ArcAttribsClass

于 2012-11-23T08:13:57.670 回答
1

如果你使用 C++11,你可以这样做:

template<class Graph, class NodeAttribs=Empty, class ArcAttribs=Empty> ...

我没有在标准中找到相关的语言,但 gcc 接受它。

于 2012-11-23T09:03:21.070 回答