Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8871625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:02:02+00:00 2026-06-14T18:02:02+00:00

Why is it impossible to use NULL as default pointer argument inside templated function?

  • 0

Why is it impossible to use NULL as default pointer argument inside templated function?
Lets concider the following code:

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

I want to be able to call it like this:

graphToGraphviz(g);

I have suspections, that compiler thinks it cannot resolve the types for NULL, but these types are not used if the attribute is NULL (there are if conditions). But maybe this case could not be resolved the proper way by compiler. If yes, how could I write such overloaded function, which will allow me to use the short form?

I have an idea of overloading it like that:

class Empty{}

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

but then the compiler gives me errors, among others, that class Empty has no operator [] defined. This is again understable, but do I have to make all these “dummy” operator overloadings and empty functions to satify compiler or is there a better way to do this?

EDIT:
Please take a look at the full source code – it converts Lemon graph to graphviz format:
I’ve tried to use the new syntax from C++11 (as the answers below suggest), but without success.

#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_ */

with C++11 syntax the function header will look like:

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      = "")

but it does not compile and gives a tons of strange errors.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:02:04+00:00Added an answer on June 14, 2026 at 6:02 pm

    If you are using C++11, you can do this:

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

    I have not found relevant language in the standard, but gcc accepts it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've always thought that it's impossible for this to be null inside instance method
It's impossible to use static variables on a session bean code. Is this restriction
It seems impossible to use the webkitRequestFileSystem method from a local file without some
I have phpMyAdmin running with MAMP, and I am finding it impossible to use
It seems impossible to define PUT when I use Html.BeginForm to submit a form
I use only C99, and, yesterday, I heard that it was impossible to mix
I want to make impossible checking checkbox in WPF (from C# code). Only unchecking
I'm getting null pointer exceptions when I try to initialize Button and EditText objects
In WPF, it seems to be impossible to select (with the mouse) a null
I wrote the following code : char arrA[] = something.; char arrB[ strlen(arrA) +

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.