question on template template parameters

Discussion in 'C++' started by REH, Jun 12, 2005.

  1. REH

    REH Guest

    GCC accepts this in a template parameter list:

    template<class> class T = std::vector

    However Visual C++ conplains because the actual template parameter list of
    std::vector does not match, because of course the defaulted allocator
    parameter. So, technically its correct. But what does the standard say?
    Does it allow the above? Stroustrup's "The C++ Programming Language" (SE)
    uses the above in several examples.

    Thanks,

    REH
     
    REH, Jun 12, 2005
    #1
    1. Advertising

  2. REH wrote:
    > GCC accepts this in a template parameter list:
    >
    > template<class> class T = std::vector
    >
    > However Visual C++ conplains because the actual template parameter list of
    > std::vector does not match, because of course the defaulted allocator
    > parameter. So, technically its correct. But what does the standard say?
    > Does it allow the above? Stroustrup's "The C++ Programming Language" (SE)
    > uses the above in several examples.


    The acceptance of this depends on the implementation of 'std::vector'.
    It is possible that the implementation has only one argument for that
    template. In that case the code is valid. It is also possible that
    the implementation has several arguments, in which case the program that
    _assumes_ only one argument is ill-formed.

    Until "template typedefs" are introduced into the language, you could
    work around the error by wrapping the 'std::vector' into another template:

    template<class T> struct vector_def_alloc {
    typedef std::vector<T> type; // the allocator used is the default
    };

    .... template<class> class T = vector_def_alloc::type ...

    (or something like that)

    V
     
    Victor Bazarov, Jun 13, 2005
    #2
    1. Advertising

  3. REH

    REH Guest

    "Victor Bazarov" <> wrote in message
    news:1ofre.4$01.us.to.verio.net...
    > The acceptance of this depends on the implementation of 'std::vector'.
    > It is possible that the implementation has only one argument for that
    > template. In that case the code is valid. It is also possible that
    > the implementation has several arguments, in which case the program that
    > _assumes_ only one argument is ill-formed.
    >
    > Until "template typedefs" are introduced into the language, you could
    > work around the error by wrapping the 'std::vector' into another template:
    >
    > template<class T> struct vector_def_alloc {
    > typedef std::vector<T> type; // the allocator used is the default
    > };
    >
    > ... template<class> class T = vector_def_alloc::type ...
    >
    > (or something like that)
    >
    > V


    Thanks Victor. Is there a better way of doing this type of thing than
    template template parameters? Basically, I want to be able to instantiate
    the class template with different types of containers, but I want a policy
    that enforces that the container's element type is a specific type. In
    other words, the container type is definable, but the element type is not.

    REH
     
    REH, Jun 13, 2005
    #3
  4. REH wrote:
    > "Victor Bazarov" <> wrote in message
    > news:1ofre.4$01.us.to.verio.net...
    >
    >>The acceptance of this depends on the implementation of 'std::vector'.
    >>It is possible that the implementation has only one argument for that
    >>template. In that case the code is valid. It is also possible that
    >>the implementation has several arguments, in which case the program that
    >>_assumes_ only one argument is ill-formed.
    >>
    >>Until "template typedefs" are introduced into the language, you could
    >>work around the error by wrapping the 'std::vector' into another template:
    >>
    >> template<class T> struct vector_def_alloc {
    >> typedef std::vector<T> type; // the allocator used is the default
    >> };
    >>
    >>... template<class> class T = vector_def_alloc::type ...
    >>
    >>(or something like that)
    >>
    >>V

    >
    >
    > Thanks Victor. Is there a better way of doing this type of thing than
    > template template parameters? Basically, I want to be able to instantiate
    > the class template with different types of containers, but I want a policy
    > that enforces that the container's element type is a specific type. In
    > other words, the container type is definable, but the element type is not.


    Did you mention "Policy"?... Somebody mentioned "policy", anyway... I
    definitely heard something that sounded like "policy"...

    If you're up to it, take a look at "Modern C++ Design". It's all about
    policies and how you use them.

    V
     
    Victor Bazarov, Jun 13, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ian
    Replies:
    5
    Views:
    569
  2. Sebastian Faust
    Replies:
    4
    Views:
    430
    Sebastian Faust
    Oct 18, 2003
  3. case2005
    Replies:
    3
    Views:
    1,859
    Nicolas Pavlidis
    Feb 13, 2005
  4. =?ISO-8859-1?Q?Erik_Wikstr=F6m?=

    Template template parameters, or something

    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Nov 10, 2005, in forum: C++
    Replies:
    3
    Views:
    308
  5. kito
    Replies:
    2
    Views:
    438
Loading...

Share This Page