Topic: Useful template template arguments?


Author: allan_w@my-dejanews.com (Allan W)
Date: Tue, 4 Feb 2003 06:40:52 +0000 (UTC)
Raw View
> Graeme Prentice schrieb:
> > >1) The C++ standard explicitely allows, that each of its class templates
> > >may contain further template arguments, with implementation defined
> > >default arguments, e.g.

dsp@bdal.de (Daniel Spangenberg) wrote
> > Where does the standard say this is allowed?

> Mea culpa, mea culpa!
>
> Red-faced, I excuse for the misleading statement (# 1). You are absolutely
> right, the C++ standard does NOT explicitely allow implementation defined
> template arguments of STL classes (whether default or not).

I think it might have said this at some point before the standard was
voted on. Perhaps even in one of the public drafts? (Just guessing though)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dsp@bdal.de (Daniel Spangenberg)
Date: Wed, 29 Jan 2003 21:23:05 +0000 (UTC)
Raw View
Hello Graeme Prentice!

Graeme Prentice schrieb:

> >1) The C++ standard explicitely allows, that each of its class templates
> >may contain further template arguments, with implementation defined
> >default arguments, e.g.
>

[snip]

>
> Where does the standard say this is allowed?
>

Mea culpa, mea culpa!

Red-faced, I excuse for the misleading statement (# 1). You are absolutely
right, the C++ standard does NOT explicitely allow implementation defined
template arguments of STL classes (whether default or not).

Actually I always DO quote the standard in statements I give, but in this
case I must have been mislead by statements of others which have left their
traces in my brain ;-))

As Howard Hinnant has pointed out (Thanks!), at least one other person has
had a similar idea but was also mislead - The corresponding anubis article
shows that this possible secenario cannot happen in an conforming
implementation.

Sorry for the trouble and unnecessary discussion I caused by this!



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: gp1@paradise.net.nz (Graeme Prentice)
Date: Sat, 25 Jan 2003 21:07:15 +0000 (UTC)
Raw View
On Fri, 24 Jan 2003 19:29:38 +0000 (UTC), dsp@bdal.de (Daniel
Spangenberg) wrote:

>
>To my mind, this is a defect in the template template argument
>deduction, because it prevents us from using STL classes as template
>template arguments.
>
>Am I mislead? Any suggestions?

[ Here's a slight correction to my previous post
- at line *************  ]


This is partly discussed in the Vandevoord and Josuttis template book
C++ Templates The Complete Guide.

You can provide a default argument for the trailing parameter(s) of a
template template parameter    e.g. section 5.4 of the above book gives
this example

template <typename T,
             template <typename ELEM,
                            typename ALLOC = std::allocator<ELEM> >
                            class CONT = std::deque>
class Stack {
CONT<T> elems;
};

You can omit ALLOC because it is not used.

Without the default argument (allocator) for the second parameter
(ALLOC), the declaration CONT<T> is illegal  - and without the second
parameter (ALLOC), you can't use the two parameter STL container classes
like vector and deque.


************************
You could do
CONT<T,std::allocator<T> > elems;
instead of the default argument, but then you don't get the allocator
provided by the supplied template class unless it happens to be
std::allocator<T>  -  which it is for vector, deque and list, but may
not be std::allocator if you supply your own container class with your
own allocator.


Graeme

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: lars_skiba@web.de (Lars Skiba)
Date: Sun, 26 Jan 2003 20:06:55 +0000 (UTC)
Raw View
Is it possible to solve that problem like that:
namespace your_ns {
template<class T, class Allocator = allocator<T> >
class vector : public std::vector<T, Allocator> {};
};
And then you use only your namespace. bad but it should work.



--
www.c-plusplus.de
Das deutsche C++ Forum

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: gp1@paradise.net.nz (Graeme Prentice)
Date: Mon, 27 Jan 2003 20:27:00 +0000 (UTC)
Raw View
On Fri, 24 Jan 2003 19:29:38 +0000 (UTC), dsp@bdal.de (Daniel
Spangenberg) wrote:

>
>Hello C++ Gurus!
>
>I would like to discuss about two competing constraints on template
>template parameters, e.g.
>
>namespace DS{
>
>template<template<typename T, typename A = allocator<T> > class C>
>class Collector
>{
>//...
>};
>
>}
>
>As I read the current C++ standard, one can NEVER write portable C++
>programs, if one chooses a container of the STL as argument of such a
>template class, because
>
>1) The C++ standard explicitely allows, that each of its class templates
>may contain further template arguments, with implementation defined
>default arguments, e.g.

Can you give a reference to the standard that supports what you are
saying here.  I don't think the standard allows the number of template
parameters for library templates to vary  - the actual number of
parameters must be exactly what is specified in the standard

e.g. class vector

namespace std {
template <class T, class Allocator = allocator<T> >
class vector {

must always have two parameters



>
>namespace std{
>
>template<class T, class A = allocator<T>, class MyHiddenParam =
>DefaultHiddenParam<T, A> >
>class vector {
>//..
>};

Where does the standard say this is allowed?



>
>}
>
>2) Argument deduction of template template arguments does NOT consider
>class template with further default arguments, so the example
>
>#include <vector>
>
>typedef DS::Collector<double, std::vector> MyCollector;
>
>is not guaranteed to compile on every conforming implementation.
>
>
>To my mind, this is a defect in the template template argument
>deduction, because it prevents us from using STL classes as template
>template arguments.
>
>Am I mislead? Any suggestions?


This is partly discussed in the Vandevoord and Josuttis template book
C++ Templates The Complete Guide.

You can provide a default argument for the trailing parameter(s) of a
template template parameter    e.g. section 5.4 of the above book gives
this example

template <typename T,
             template <typename ELEM,
                            typename ALLOC = std::allocator<ELEM> >
                            class CONT = std::deque>
class Stack {
CONT<T> elems;
};

You can omit ALLOC because it is not used.

Without the default argument (allocator) for the second parameter
(ALLOC), the declaration CONT<T> is illegal  - and without the second
parameter (ALLOC), you can't use the two parameter STL container classes
like vector and deque.

You could do
CONT<T,std::allocator<ELEM>> elems;
instead of the default argument I think.  The default argument for ALLOC
does *NOT* allow you to supply a one argument template to Stack
(according to GCC 3.2 and Comeau 4.3.0.1 anyway)

e.g. the first line in main() below, won't compile.  You also cannot
supply s1 as the default template for c1.


#include <iostream>
#include <ostream>

template <typename T>
struct s1
{
    s1(){ std::cout << "\ns1 " << T(65); }
};

template <typename T1,typename T2=float>
struct s2
{
    s2(){ std::cout << "\ns2 "
          << T1(66) << ' ' << T2(67); }
};

template <typename T,
        template <typename U1,typename U2=double>
        class c1 = s2   >
struct s3
{
    c1<T> o1;
    c1<T,T> o2;
    c1<T,int> o3;
};

int main()
{
    s3<signed char,s1> o4;  // illegal
    s3<unsigned char,s2> o5;
    s3<float> o6;
}

Graeme

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dsp@bdal.de (Daniel Spangenberg)
Date: Fri, 24 Jan 2003 19:29:38 +0000 (UTC)
Raw View
Hello C++ Gurus!

I would like to discuss about two competing constraints on template
template parameters, e.g.

namespace DS{

template<template<typename T, typename A = allocator<T> > class C>
class Collector
{
//...
};

}

As I read the current C++ standard, one can NEVER write portable C++
programs, if one chooses a container of the STL as argument of such a
template class, because

1) The C++ standard explicitely allows, that each of its class templates
may contain further template arguments, with implementation defined
default arguments, e.g.

namespace std{

template<class T, class A = allocator<T>, class MyHiddenParam =
DefaultHiddenParam<T, A> >
class vector {
//..
};

}

2) Argument deduction of template template arguments does NOT consider
class template with further default arguments, so the example

#include <vector>

typedef DS::Collector<double, std::vector> MyCollector;

is not guaranteed to compile on every conforming implementation.


To my mind, this is a defect in the template template argument
deduction, because it prevents us from using STL classes as template
template arguments.

Am I mislead? Any suggestions?



With greetings from Germany,

Daniel Spangenberg



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: loic.actarus.joly@wanadoo.fr (=?ISO-8859-1?Q?Lo=EFc_Joly?=)
Date: Fri, 24 Jan 2003 22:28:52 +0000 (UTC)
Raw View
Daniel Spangenberg wrote:
> Hello C++ Gurus!
I am not one, but will still give my opinion...

> 1) The C++ standard explicitely allows, that each of its class template=
s
> may contain further template arguments, with implementation defined
> default arguments, e.g.
>=20
> 2) Argument deduction of template template arguments does NOT consider
> class template with further default arguments, so the example
>=20
> To my mind, this is a defect in the template template argument
> deduction, because it prevents us from using STL classes as template
> template arguments.
>=20
> Am I mislead? Any suggestions?

I think this problem can also occur in other places, for example=20
template partial specialisation.

I believe at one time people proposed to remove the possibility to add=20
specific template arguments to standard template, but I do not know the=20
current status of this issue.

--=20
Lo=EFc

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: hinnant@metrowerks.com (Howard Hinnant)
Date: Sat, 25 Jan 2003 21:01:15 +0000 (UTC)
Raw View
In article <b0sdjm$gne$1@news-reader12.wanadoo.fr>, Lo=EFc Joly
<loic.actarus.joly@wanadoo.fr> wrote:

| I believe at one time people proposed to remove the possibility to add=20
| specific template arguments to standard template, but I do not know the=
=20
| current status of this issue.

Please see:

http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-closed.html#94

--=20
Howard Hinnant
Metrowerks

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]