[RESOLVED] Equivalance Typeof Operator Suggestion
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] Equivalance Typeof Operator Suggestion

  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    [RESOLVED] Equivalance Typeof Operator Suggestion

    Hello to all expert, i have a class template like this.

    Code:
    template <typename T, class edgeType>
    class graph;
    I would like to implement MF differently when different type of edgeType is supplied, for instance, arcs was supplied, then i identify this as directed graph and i just insert one set of edge.

    If edge, then i insert two sets of edge.

    Any typeof operator in C++ STL ?

    Thanks.
    Thanks for your help.

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: Equivalance Typeof Operator Suggestion

    Partial specialisation?

    Code:
    struct arcType
    {
    };
    
    template <typename T, class edgeType>
    class graph
    {
    };
    
    template <typename T>
    class graph<T, arcType>
    {
    };
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Equivalance Typeof Operator Suggestion

    There&#180;s a typeid operator that relies on RTTI and provides type information at runtime. Providing specialized templates allows compile time decision, and that&#180;s probably what you want.
    You can&#180;t specialize member templates and you can&#180;t partially specialize non member function, but you can use a (partially) specialized inserter template.

    Code:
    // generic Edge Inserter
    template<typename Container, typename EdgeType>
    struct EdgeInserter
    {
       void operator()( Container& c, const EdgeType& e )
       {
          // insert edge into container 
          c.push_back( e );
       }
    };
    
    // specialized Edge Inserter
    template<typename Container>
    struct EdgeInserter<Container,Arc>
    {
       void operator()( Container&c, const Arc& a )
       {
          // insert arc into container
          c.push_back( a );
       }
    };
    
    
    template<typename T, typename EdgeType>
    struct graph
    {
       EdgeInserter<EdgeContainerType,EdgeType> Inserter;
    
       // Container to hold edges (whatever this is)
       EdgeContainerType Edges;
    
       void add_edge( const EdgeType& NewEdge )
       {
          // use inserter to add edge
          Inserter( Edges, NewEdge );
       }
    };
    Last edited by GNiewerth; February 12th, 2009 at 05:43 AM.

  4. #4
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Equivalance Typeof Operator Suggestion

    Boost has a typeof macro. gcc supports it natively and its emulated elsewhere. Its here.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  5. #5
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Equivalance Typeof Operator Suggestion

    GNiewerth, wonder approach. I will try it out when i come back from university.

    A billion thanks to you.
    Thanks for your help.

  6. #6
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Equivalance Typeof Operator Suggestion

    I think a bit shorter if you use functor CT polymorphism.
    My approach will be a Non MF that called appropriate MF.

    Question 1:
    What is the pro and cons using functor CT polymorphism ?

    The thinking logic is from based on the edge type then coded different implement strategy.
    I never think this out. Now i know.

    A billion thanks for your help.
    Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center