CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Templates: why can't I do this?

    Code:
    #include <list>
    
    template<class C, class T>
    class MyContainer
    {
    private:
    	C<T> m_container;
    };
    
    class A
    {
    };
    
    int main()
    {
    	MyContainer<std::list, A*> container();
    
    	return 0;
    }
    It doesn't like the line "C<T> m_container;" and gives me the error "error C2059: syntax error : '<'".


    Jeff

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449
    std::list is not a type, it is a template. The following code works correctly.
    Code:
    #include <list>
    
    template<typename C>
    class MyContainer
    {
    private:
    	C m_container;
    };
    
    class A
    {
    };
    
    int main()
    {
    	MyContainer<std::list<A*> > container(); // Remember to put a space between the "> >"
    
    	return 0;
    }
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Paul,

    I was just about to post the same thing, with one additional typedef:

    Code:
    #include <list>
    #include <set>
    
    template<class C>
    class MyContainer
    {
    public:
    	typedef C::value_type value_type;
    private:
    	C m_container;
    };
    
    class A
    {
    };
    
    int main()
    {
    	MyContainer<std::list<A*> > myList();
    	MyContainer<std::set<A*> > mySet();
    
    	return 0;
    }
    With the typedef for value_type defined in the std:: container , it's possible to return items in the container.

    Thanks for the reply. I've found a good solution. However, I'm still not clear on why my original post does not work.

    Jeff

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449
    The reason why it doesn't work is that a template argument is looking for a concrete type, for example, int, double, char, std::string, std::list<int>, std::list<A*>, etc.

    std::list is not a type. It is a template class that defines a family of types. However, std::list<int> is a concrete type, since you are describing a list of integers.

    And BTW, your int main() function now has declared two functions, not instances:
    Code:
    int main()
    {
    	MyContainer<std::list<A*> > myList();  // Function returning a MyContainer<std::list<A*> >
    	MyContainer<std::set<A*> > mySet(); // Function returning a MyContainer<std::set<A*> >
    
    	MyContainer<std::list<A*> > myList2;  // Instance of MyContainer<std::list<A*> >
    	MyContainer<std::set<A*> > mySet2; // Instance of MyContainer<std::set<A*> >
    }
    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    It's starting to make sense. Thanks for the reply.

    And yes, I noticed my function declarations. Well, I noticed them as soon as I tried to use them .

    Jeff

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Doesn't ANSI C++ allow
    Code:
    template<template <class> class C, class T>
    class MyContainer
    {
        // blah, blah
    
        C<T> m_container;
    };
    
    int main()
    {
        MyContainer<std::list, int> mc;
    }
    Of course, VC++ doesn't support that syntax, but who's surprised by that?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #7
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    Can somebody try this on a better compiler?

    Jeff

  8. #8
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    This compiles with Comeau:
    Code:
    #include <list>
    
    template<template <class, class> class C, class T>
    class MyContainer
    {
        // blah, blah
    
        C<T, std::allocator<T> > m_container;
    };
    
    int main()
    {
        MyContainer<std::list, int> mc;
    }
    Note that I had to change it a bit because list actually has two template parameters.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  9. #9
    Join Date
    Apr 1999
    Posts
    27,449
    Thanks Graham,

    I knew that there are now template-template parameters, but didn't go to Comeau to try out the new syntax.

    Regards,

    Paul McKenzie

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured