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

  • Home
  • SEARCH
  • 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 7602265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:22:07+00:00 2026-05-30T23:22:07+00:00

Basically while trying to implement a graph through adjacency list, I get stucked defining

  • 0

Basically while trying to implement a graph through adjacency list, I get stucked defining a graph node:

template <
    typename Vertex, 
    typename Edge,
    typename EdgeLink = std::pair< Edge, GraphNode* >,
    typename Alloc = std::allocator< EdgeLink >
>
class GraphNode
{
public:
    Vertex vertex;
    std::list< EdgeLink, Alloc > neighbours;
};

I realize that I can’t give parameters to GraphNode template pointer, because they are not defined yet. My question to c++ templates gurus is: what technique is used in this case?

Thanks.

  • 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-05-30T23:22:08+00:00Added an answer on May 30, 2026 at 11:22 pm

    Precising the allocator does not necessitate to precise what the allocator can be used for. For example, in a std::list<T> the allocator passed is std::allocator<T> and yet the list will allocate _ListNode<T> (implementation defined). This is because allocators need to provide a rebind mechanism.

    template <
        typename Vertex, 
        typename Edge,
        typename Allocator = std::allocator<void*>
    >
    class GraphNode
    {
    public:
        typedef GraphNode<Vertex, Edge, Allocator> NodeType;
        typedef std::pair< Edge, NodeType* > LinkType;
        typedef typename Allocator::template rebind<LinkType>::other AllocatorType;
    
        Vertex vertex;
        std::list< LinkType, AllocatorType > neighbours;
    };
    

    In action at ideone.

    Note that even though list will do a rebind itself, you should still do it, because the allocator types reference and pointer (and their const version) will be pulled as typedef inside the list.

    EDIT: Allow container specification.

    This is tricky because the allocator is unfortunately only defined once you are within GraphNode, you thus need to pass it to the container only within the class, and thus cannot use it in the template outside.

    This means using template template parameters, and we thus need to “fix” the arity. Since both vector and list only take two parameters we are lucky here, but it might not always hold… fortunately with C++11 allowing template aliases, it won’t be too harsh a requirement for the user.

    template <
        typename Vertex, 
        typename Edge,
        template <typename, typename> class Container = std::vector,
        typename Allocator = std::allocator<void*>
    >
    class GraphNode
    {
    public:
        typedef GraphNode<Vertex, Edge, Container, Allocator> NodeType;
        typedef std::pair< Edge, NodeType* > LinkType;
        typedef typename Allocator::template rebind<LinkType>::other AllocatorType;
        typedef Container<LinkType, AllocatorType> NeighboursType;
    
        Vertex vertex;
        NeighboursType neighbours;
    };
    

    This can be invoked so:

    GraphNode<std::string, int>
    GraphNode<std::string, int, std::list>
    GraphNode<std::string, int, std::vector>
    

    Demo.

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

Sidebar

Related Questions

I recently ran across this problem while trying to implement a service that has
I am getting this exception while trying to call SOAP webservice using axis. basically
I am trying to implement macro replacement based on this discussion . Basically it
I'm having problem (basically, I'm confused.) while trying to create a worker thread for
I am trying to implement user-friendly URLS, while keeping the existing routes, and was
I've be struggling for a while trying to make it work. Basically I have
I've been stuck for quite a while now trying to get this query to
basically, i was wiresharking packets on my PS3 while viewing Motorstorm Leaderboards. The leaderboards
I'm getting an Illegal characters in path error while using XMLTextReader method. Basically, I'm
I have a 'template' system in my CMS rails app. Basically the whole HTML

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.