Null作为默认模板参数

时间:2012-11-23 08:07:47

标签: c++ templates c++11 overloading default

为什么在模板化函数中使用NULL作为默认指针参数是不可能的? 让concider得到以下代码:

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

我希望能够这样称呼它:

graphToGraphviz(g);

我有暂停,编译器认为它无法解析NULL的类型,但如果属性为NULL(有条件),则不使用这些类型。但也许这种情况无法通过编译器以正确的方式解决。如果是,我怎么能写这样的重载函数,这将允许我使用简短形式?

我知道像这样重载:

class Empty{}

template<class Graph> string
graphToGraphViz(Graph       &graph,
                string      name      = ""){
    return graphToGraphviz<Graph, Empty, Empty>(graph, NULL, NULL, name)
}

但是编译器给了我错误,其中,类Empty没有定义operator []。这又是可以理解的,但我是否必须使所有这些“虚拟”运算符重载和空函数满足编译器,或者有更好的方法来做到这一点吗?

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

但它没有编译并且给出了大量奇怪的错误。

3 个答案:

答案 0 :(得分:2)

编译时遇到问题:

graphToGraphviz(g);

现在NodeAttribsArcAttribs的类型是什么? 编译器必须推断其类型,无论您是否使用它。因为使用或不使用是运行时检查 使用您当前的代码,上述类型将变为不可导出

  

我怎么能写出这样的重载功能

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

template<class Graph>
string graphToGraphviz(Graph &graph, string name = "");

答案 1 :(得分:1)

你试过吗

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

OR

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

NodeAttribsClassArcAttribsClass哪些是可以在该广告位中使用的有效(具体)类?

答案 2 :(得分:1)

如果您使用的是C ++ 11,则可以执行以下操作:

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

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