One template question

This is a discussion on One template question within the C++ Programming forums, part of the General Programming Boards category; Code: template <typename T, typename CONT /*= std::vector*/ > struct Stack{ CONT elem; }; The above code compiles fine as ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    One template question

    Code:
    template <typename T, typename CONT /*= std::vector*/ > 
    struct Stack{
        CONT elem;
    };
    The above code compiles fine as it should be If I just uncomment the std::vector one it fires compilation errors saying
    Code:
    error: expected type-specifier
    error: expected '&gt;'
    But I don't find any syntactic or symantic error in the above code. can anybody please point it out.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    std::vector of what?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,114
    I am guessing that you actually want to write:
    Code:
    template <typename T, typename CONT = std::vector<T> > 
    struct Stack{
        CONT elem;
    };
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    std::vector is unspecialized.
    std::vector<T> in specialized, thus

    template <typename T, typename CONT = std::vector>
    ...does not work.

    template < typename T, typename CONT = std::vector<T> >
    ...works.

    and
    template <typename T, template<typename> CONT = std::vector>
    ...also works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Thanks all understood
    I forgot that std::vector does not name a type rather std::vector<SOMETYPE> is a Typename (Silly mistake)

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by Elysia View Post
    template <typename T, template<typename> CONT = std::vector>
    ...also works.
    No, it doesn't. std::vector takes an unspecified number of template arguments, but at least 2.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    No, it doesn't. std::vector takes an unspecified number of template arguments, but at least 2.
    What do you mean it takes an unspecified number of template arguments? How is that possible? Can you use the ... operator in template arguments?
    As far as I can see, it just takes 2 arguments, one of which is defaulted.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The standard says that the implementation may give the containers any number of template arguments after those specified by the standard, as long as they have defaults.

    This, combined with disallowing the substitution of default arguments in template template parameters, has led to many unhappy users.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    Tsch. Burned.
    Oh well.
    At least the second works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 09:44 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. another template question
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2008, 03:52 PM
  4. Quick question about class template
    By merixa in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2005, 11:43 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21