Topic: template template parameter - is it legal?


Author: Herb Sutter <hsutter@acm.org>
Date: 15 Feb 2002 06:21:32 -0500
Raw View
On 7 Feb 2002 00:57:00 GMT, neurosion@yahoo.com (Russ) wrote:
>Allow me to correct myself.  This used to be found in MSDN Q243451.
>After a half-hearted search of msdn.microsoft.com, it would appear
>that Microsoft has chosen to stop publicizing their ANSI noncompliance
>issues.

Did you look very hard? I just went to http://msdn.microsoft.com, searched
for "standard compliance", and the very first hit was:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/vclrfStandardComplianceIssuesInVisualC.asp

I'll paste the synopsis here:

--- snip ---
Standard Compliance Issues in Visual C++

The following topics are some of the known places where the Visual C++
implementation of C++ does not agree with the C++ standard. The section
numbers refer to section numbers in the C++ standard.

Compiler Limits
2.11 Alternative Keyword Representations
3.4.2 Argument-Dependent Name Lookup
6.4 Scope of a Name Declaration in a Selection Statement
14 export Keyword on a Template
14.5.2 Out-of-Class Template Definitions
14.5.4 Class Template Partial Specialization
14.5.5.2 Partial Ordering of Function Templates
15.4 Function Exception Specifiers
18.6.4 The uncaught_exception Function
--- snip ---

Herb

---
Herb Sutter (www.gotw.ca)

Secretary, ISO WG21/ANSI J16 (C++) standards committee   (www.gotw.ca/iso)
Contributing Editor, C/C++ Users Journal                     (www.cuj.com)

Check out "THE C++ Seminar" - Boston, March 2002 (www.gotw.ca/cpp_seminar)
---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: "Igor A. Goussarov" <igusarov@akella.com>
Date: Mon, 11 Feb 2002 19:21:16 GMT
Raw View
Oleg Petsa wrote:
>
> That is the problem: the standard does not says a word whether default
> template arguments are considered or not, thus allowing the compiler
> producers to decide on their whim.

   I tend to agree that ignoring default value will be more consistent
with the way default values of function arguments are treated.

Igor

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: google@vandevoorde.com (Daveed Vandevoorde)
Date: 13 Feb 02 01:20:22 GMT
Raw View
plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1013447645 6681 127.0.0.1 (11 Feb 2002 17:14:05 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: 11 Feb 2002 17:14:05 GMT
Content-Length: 389
ReSent-Date: Mon, 11 Feb 2002 09:47:00 -0800 (PST)
ReSent-From: Steve Clamage <clamage@eng.sun.com>
ReSent-To:  <c++-submit@netlab.cs.rpi.edu>
ReSent-Subject: Re: template template parameter - is it legal?
ReSent-Message-ID: <Pine.SOL.4.33.0202110947000.4516@taumet>

Oleg Petsa <oleg.petsa@alcatel.de> wrote:
[...]
> IMHO, you are wrong when you say:
> "IOW, Stroustrup and Cantata++ are not contradicting each other.",
> because Stroustrup uses default template argument in the same way, as I
> (you can look at Chapter C.13.3 in "TC++PL", 3rd edition for a proof of
> my words.

You're correct; my apologies for not actually checking the text.

 Daveed



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]




Author: google@vandevoorde.com (Daveed Vandevoorde)
Date: 7 Feb 2002 21:14:32 GMT
Raw View
plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1013034262 15627 127.0.0.1 (6 Feb 2002 22:24:22 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: 6 Feb 2002 22:24:22 GMT
Content-Length: 2416
ReSent-Date: Thu, 7 Feb 2002 07:56:41 -0800 (PST)
ReSent-From: Steve Clamage <clamage@eng.sun.com>
ReSent-To:  <c++-submit@netlab.cs.rpi.edu>
ReSent-Subject: Re: template template parameter - is it legal?
ReSent-Message-ID: <Pine.SOL.4.33.0202070756410.3870@taumet>

Oleg Petsa <oleg.petsa@alcatel.de> wrote:
[...]
> I have a problem with the so called "template template parameter".
> The following code is compiled by g++ without any errors and warnings
> (except for warnings for "use of old-style cast" in STL headers), but
> rejected by several other C++ compilers (MS Visual C++, Sun CC) and by
> IPL Cantata++ test tool:

Your code in not valid standard C++.  In fact, this issue was brought
to the attention of the C++ standardization committee (it is known as
"Core issue 150").

The problem (as you might have noticed) is that std::list has more
than one parameter (though you can get away with specifying just one
argument because the other arguments are defaulted).

Remember however that the parameters of template template parameters
can also have default template arguments.  So you example could start
with:

 template<typename T,
                 template<typename T,
                          typename Allocator = std::allocator<T> >
                 class Cont>
 class TemplateContainer ...

which perhaps may address your needs (modulo typos ;-).

> template< class T, template<class> class Cont >
> class TemplateContainer {
> protected:
>   Cont<T> _cont;
> public:
>   typedef typename Cont<T>::iterator Iterator;
>
>   Iterator begin() { return _cont.begin(); };
>   void pushBack(const T& t) { _cont.push_back(t); };
>   //...
>   //...
> };
>
> #include <list>
> using std::list;
>
> class  IntContainer : public TemplateContainer< int, list > {};
>
> int main(void)
> {
>   IntContainer  intContainer;
>
>   intContainer.pushBack(20);
>   //...
> };
>
>
> The error message looks like:
>
> Cantata++ CPPSM  v2.2-FR01315 (c) 2001 IPL Information Processing Ltd
> Error SM10019: "TemplContTest.cpp", line NN: class template "list" is
> not compatible with template template parameter "Cont"
> class  IntContainer : public TemplateContainer< int, list > {};
>                                                      ^
>
>
> The people at IPL mean, that such an example is illegal C++.
> But Stroustrup has described such "template template parameter"
> construct in his book "The C++ Programming Language", chapter C.13.3
> "Templates as Template Parameter".

Note that Cantata++ isn't saying you cannot have template template
parameters: it is saying there is a mismatch.  IOW, Stroustrup and
Cantata++ are not contradicting each other.

 Daveed



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Carl Daniel" <cpdaniel@pacbell.net>
Date: 7 Feb 2002 21:14:19 GMT
Raw View
"Oleg Petsa" <oleg.petsa@alcatel.de> wrote in message
news:3C5FDE90.E2C70EBA@alcatel.de...
>
> Hello all!
>
> I have a problem with the so called "template template parameter".
> The following code is compiled by g++ without any errors and warnings
> (except for warnings for "use of old-style cast" in STL headers), but
> rejected by several other C++ compilers (MS Visual C++, Sun CC) and by
> IPL Cantata++ test tool:

VC6 doesn't do template template parameters at all, so forget about that
one.

>
>
[snipped example]
>
>
> The error message looks like:
>
> Cantata++ CPPSM  v2.2-FR01315 (c) 2001 IPL Information Processing Ltd
> Error SM10019: "TemplContTest.cpp", line NN: class template "list" is
> not compatible with template template parameter "Cont"
> class  IntContainer : public TemplateContainer< int, list > {};

The compiler is correct in calling this an error, unfortunately.  The
complete signature of std::list is template<typename T, typename
Allocator=allocator<T> > class list {};

That's not compatible with a template template parameter declared as
template<class>.  Unfortunately, the standard defines it to be this way -
IMO it makes template template parameters nearly worthless, but that's what
the standard says.

-cd



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Igor A. Goussarov" <igusarov@akella.com>
Date: 7 Feb 2002 21:14:21 GMT
Raw View
Oleg Petsa wrote:
>
> template< class T, template<class> class Cont >
> class TemplateContainer;
>
> #include <list>
> using std::list;
>
> class  IntContainer : public TemplateContainer< int, list > {};

std::list is declared as

template <typename T, typename Allocator = allocator<T> >
class list;

And therefore its template signature is "template <class, class>", which
is obviously incompatible with your template template parameter 'Cont'.
And I cannot find a place in the standard that explicitly states whether
default arguments are considered when matching template template
parameters.

Igor



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Oleg Petsa <oleg.petsa@alcatel.de>
Date: 7 Feb 2002 21:14:34 GMT
Raw View
> >The people at IPL mean, that such an example is illegal C++.
> >But Stroustrup has described such "template template parameter"
> >construct in his book "The C++ Programming Language", chapter C.13.3
> >"Templates as Template Parameter".
> >
> >So, tell me, please,
> >whether the code is illegal (in which case Stroustrup and g++ are
> >wrong),
> >or it is legal (in which case the most C++ compilers are wrong)?
>
> I think that you have two questions.  The first is whether
> or not template template parameters are allowed.  The answer to
> that is yes.

Thanks for reply.
I really mean just the 2nd question (I've read the begining of Chapter
14 of the C++ Standard, and know, that template template parameters are
allowed).

> The second is whether your code above is allowed.
> The answer to that is no.  Why?  Because std::list has a different
> number of arguments than Cont has.

So, you say that Stroustrup is wrong in his explanation of the subject
(Chapter C.13.3 of "TC++PL", 3rd Edition).

IMHO, with your reasoning ("because std::list has a different number of
arguments than Cont has") is everything OK, except that the 2nd
parameter of std::list has default value.

The problem with the most compirers is treating template template
parameters with default values.
Stroustrup has described exactly the same construct and the use of it.
And ISO C++ Standard allows such declaration "template
<class> class X". In my opinion, "<class>" (without name) should mean
"one template parameter, or one template parameter without default value
and any count of template params with default values". Otherwise, IMHO,
declaration "<class>" (without name) does not have sense.

I suppose, the important is the place where compiler checks for template
parameters.
If it is where the factual parameters are substituted:
   class  IntContainer : public TemplateContainer< int, list > {};
                                                        ^
then compiler detects a mismatch in the number of parameters ("Cont" has
1 param, "list" has 2).

But if the place of check is at the point of 1st use "Cont":
  template< class T, template<class> class Cont >
  class TemplateContainer {
  protected:
    Cont<T> _cont;
    ^    ^
then everything is OK (because factual parameter "list" can be
instantiated with 1 parameter).



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: tom_usenet@hotmail.com (tom_usenet)
Date: Fri, 8 Feb 2002 01:10:15 GMT
Raw View
On Thu,  7 Feb 2002 15:31:34 GMT, Oleg Petsa <oleg.petsa@alcatel.de>
wrote:

>typedef results in the same error message. Really, it's irrelevant here,
>whether typedef or derivation. The problem is template with default
>arguments.

Yes, it is irrelevent to the error, but I was just pointing out that
you were abusing inheritence.

>> >But Stroustrup has described such "template template parameter"
>> >construct in his book "The C++ Programming Language", chapter C.13.3
>> >"Templates as Template Parameter".
>>
>> And he is right.
>
>Excuse me, but Stroustrup and IPL cannot be both right. Have you read
>the chapter C.13.3 of 3rd Edition of "The C++ Programming Language"? If
>yes, you will notice, that Stroustrup's construct and use of it are the
>same as in my example. He declares formal parameter "Cont" as template
>with 1 unnamed parameter, and supplies factual parameter "list", which
>has 2 template parameters, but the 2nd param has default value (so that
>"list" can be instantiated with 1 parameter).

My mistake, I didn't read it. Stroustrup is wrong it seems.

>
>> Neither is quite true. Stroustrup and IPL are right, g++ is wrong.
>
>Again, Stroustrup and g++ mean the same, they should be both right or
>both wrong.

Both wrong, and I can't see it in any of the errata for Stroustrup
either. You can read a discussion of the issue at
http://www.comeaucomputing.com/iso/cwg_closed.html#150

Tom

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Oleg Petsa <oleg.petsa@alcatel.de>
Date: Fri, 8 Feb 2002 17:44:49 GMT
Raw View

tom_usenet wrote:
>
> On Thu,  7 Feb 2002 15:31:34 GMT, Oleg Petsa <oleg.petsa@alcatel.de>
> wrote:
>
> >typedef results in the same error message. Really, it's irrelevant here,
> >whether typedef or derivation. The problem is template with default
> >arguments.
>
> Yes, it is irrelevent to the error, but I was just pointing out that
> you were abusing inheritence.

I totally agree with you (but that was just an example).

> >Again, Stroustrup and g++ mean the same, they should be both right or
> >both wrong.
>
> Both wrong, and I can't see it in any of the errata for Stroustrup
> either. You can read a discussion of the issue at
> http://www.comeaucomputing.com/iso/cwg_closed.html#150

Thanks for info.

Best regards,

Oleg

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Oleg Petsa <oleg.petsa@alcatel.de>
Date: 9 Feb 2002 16:08:37 GMT
Raw View

> > template< class T, template<class> class Cont >
> > class TemplateContainer;
> >
> > #include <list>
> > using std::list;
> >
> > class  IntContainer : public TemplateContainer< int, list > {};
>
> std::list is declared as
>
> template <typename T, typename Allocator = allocator<T> >
> class list;
>
> And therefore its template signature is "template <class, class>", which
> is obviously incompatible with your template template parameter 'Cont'.

Of course, you are right, if the compiler checks for parameter
compatibility at the point of specialization. But if the check is
postponed to the point of 1st use of "Cont" (which look like "Cont<T>
_cont;"), then compiler can take default arguments into consideration.


> And I cannot find a place in the standard that explicitly states whether
> default arguments are considered when matching template template
> parameters.

Exactly. I could not find it also (that's why I posted my question
here).
That is the problem: the standard does not says a word whether default
template arguments are considered or not, thus allowing the compiler
producers to decide on their whim.



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Oleg Petsa <oleg.petsa@alcatel.de>
Date: 9 Feb 2002 16:08:36 GMT
Raw View
> Your code in not valid standard C++.  In fact, this issue was brought
> to the attention of the C++ standardization committee (it is known as
> "Core issue 150").

Thanks for info.

> The problem (as you might have noticed) is that std::list has more
> than one parameter (though you can get away with specifying just one
> argument because the other arguments are defaulted).

I have noticed that.

> Remember however that the parameters of template template parameters
> can also have default template arguments.  So you example could start
> with:
>
>         template<typename T,
>                  template<typename T,
>                           typename Allocator = std::allocator<T> >
>                  class Cont>
>         class TemplateContainer ...
>
> which perhaps may address your needs (modulo typos ;-).

Thank for the idea, I shall try it.


> > Cantata++ CPPSM  v2.2-FR01315 (c) 2001 IPL Information Processing Ltd
> > Error SM10019: "TemplContTest.cpp", line NN: class template "list" is
> > not compatible with template template parameter "Cont"
> > class  IntContainer : public TemplateContainer< int, list > {};
> >                                                      ^
> >
> >
> > The people at IPL mean, that such an example is illegal C++.
> > But Stroustrup has described such "template template parameter"
> > construct in his book "The C++ Programming Language", chapter C.13.3
> > "Templates as Template Parameter".
>
> Note that Cantata++ isn't saying you cannot have template template
> parameters: it is saying there is a mismatch.  IOW, Stroustrup and
> Cantata++ are not contradicting each other.

IMHO, you are wrong when you say:
"IOW, Stroustrup and Cantata++ are not contradicting each other.",
because Stroustrup uses default template argument in the same way, as I
(you can look at Chapter C.13.3 in "TC++PL", 3rd edition for a proof of
my words.

Best regards,

Oleg



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Oleg Petsa <oleg.petsa@alcatel.de>
Date: 6 Feb 2002 02:16:51 GMT
Raw View
Hello all!

I have a problem with the so called "template template parameter".
The following code is compiled by g++ without any errors and warnings
(except for warnings for "use of old-style cast" in STL headers), but
rejected by several other C++ compilers (MS Visual C++, Sun CC) and by
IPL Cantata++ test tool:


template< class T, template<class> class Cont >
class TemplateContainer {
protected:
  Cont<T> _cont;
public:
  typedef typename Cont<T>::iterator Iterator;

  Iterator begin() { return _cont.begin(); };
  void pushBack(const T& t) { _cont.push_back(t); };
  //...
  //...
};

#include <list>
using std::list;

class  IntContainer : public TemplateContainer< int, list > {};

int main(void)
{
  IntContainer  intContainer;

  intContainer.pushBack(20);
  //...
};


The error message looks like:

Cantata++ CPPSM  v2.2-FR01315 (c) 2001 IPL Information Processing Ltd
Error SM10019: "TemplContTest.cpp", line NN: class template "list" is
not compatible with template template parameter "Cont"
class  IntContainer : public TemplateContainer< int, list > {};
                                                     ^


The people at IPL mean, that such an example is illegal C++.
But Stroustrup has described such "template template parameter"
construct in his book "The C++ Programming Language", chapter C.13.3
"Templates as Template Parameter".

So, tell me, please,
whether the code is illegal (in which case Stroustrup and g++ are
wrong),
or it is legal (in which case the most C++ compilers are wrong)?

Best regards,

Oleg Petsa



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: neurosion@yahoo.com (Russ)
Date: 7 Feb 2002 00:57:00 GMT
Raw View
plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1013013391 5070 127.0.0.1 (6 Feb 2002 16:36:31 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: 6 Feb 2002 16:36:31 GMT
Content-Length: 944
ReSent-Date: Wed, 6 Feb 2002 11:27:52 -0800 (PST)
ReSent-From: Steve Clamage <clamage@eng.sun.com>
ReSent-To:  <c++-submit@netlab.cs.rpi.edu>
ReSent-Subject: Re: template template parameter - is it legal?
ReSent-Message-ID: <Pine.SOL.4.33.0202061127520.2821@taumet>

Oleg Petsa <oleg.petsa@alcatel.de> wrote in message news:<3C5FDE90.E2C70EBA@alcatel.de>...
> Hello all!
>
> I have a problem with the so called "template template parameter".
> The following code is compiled by g++ without any errors and warnings
> (except for warnings for "use of old-style cast" in STL headers), but
> rejected by several other C++ compilers (MS Visual C++, Sun CC) and by
> IPL Cantata++ test tool:
>
>
> template< class T, template<class> class Cont >
> class TemplateContainer {
   ... implementation snipped ...
> };

I can speak only to VC++, which does not support template template
parameters.  A full list of Microsoft deviations from the standard
(it's a long list) can be found in MSDN Q243451.

Allow me to correct myself.  This used to be found in MSDN Q243451.
After a half-hearted search of msdn.microsoft.com, it would appear
that Microsoft has chosen to stop publicizing their ANSI noncompliance
issues.



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: comeau@panix.com (Greg Comeau)
Date: 7 Feb 2002 00:57:02 GMT
Raw View
In article <3C5FDE90.E2C70EBA@alcatel.de>,
Oleg Petsa  <oleg.petsa@alcatel.de> wrote:
>I have a problem with the so called "template template parameter".
>The following code is compiled by g++ without any errors and warnings
>(except for warnings for "use of old-style cast" in STL headers), but
>rejected by several other C++ compilers (MS Visual C++, Sun CC) and by
>IPL Cantata++ test tool:
>
>template< class T, template<class> class Cont >
>class TemplateContainer {
>...<snipped>..
>};
>
>#include <list>
>using std::list;
>
>class  IntContainer : public TemplateContainer< int, list > {};
>
>int main(void)
>{
>  IntContainer  intContainer;
>
>  intContainer.pushBack(20);
>  //...
>};
>
>The error message looks like:
>
>Cantata++ CPPSM  v2.2-FR01315 (c) 2001 IPL Information Processing Ltd
>Error SM10019: "TemplContTest.cpp", line NN: class template "list" is
>not compatible with template template parameter "Cont"
>class  IntContainer : public TemplateContainer< int, list > {};
>                                                     ^
>The people at IPL mean, that such an example is illegal C++.
>But Stroustrup has described such "template template parameter"
>construct in his book "The C++ Programming Language", chapter C.13.3
>"Templates as Template Parameter".
>
>So, tell me, please,
>whether the code is illegal (in which case Stroustrup and g++ are
>wrong),
>or it is legal (in which case the most C++ compilers are wrong)?

I think that you have two questions.  The first is whether
or not template template parameters are allowed.  The answer to
that is yes.  The second is whether your code above is allowed.
The answer to that is no.  Why?  Because std::list has a different
number of arguments than Cont has.
--
Greg Comeau  GA BETA:4+ New Windows Backends  PLUS 'export' beta online!
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout
World Class Compilers:  Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: tom_usenet@hotmail.com (tom_usenet)
Date: 7 Feb 2002 00:57:01 GMT
Raw View
On 6 Feb 2002 02:16:51 GMT, Oleg Petsa <oleg.petsa@alcatel.de> wrote:

>
>Hello all!
>
>I have a problem with the so called "template template parameter".
>The following code is compiled by g++ without any errors and warnings
>(except for warnings for "use of old-style cast" in STL headers), but
>rejected by several other C++ compilers (MS Visual C++, Sun CC) and by
>IPL Cantata++ test tool:
>
>
>template< class T, template<class> class Cont >

Cont must be a template that takes one parameter.

>class TemplateContainer
....
>class  IntContainer : public TemplateContainer< int, list > {};

std::list is a template that takes two parameters, one of which
happens to have a default value. The default parameter is irrelevent
when it comes to template template parameters, although some have
argued against this. Hence the above is an error. Also, why use
derivation when you meant a typedef?

>The error message looks like:
>
>Cantata++ CPPSM  v2.2-FR01315 (c) 2001 IPL Information Processing Ltd
>Error SM10019: "TemplContTest.cpp", line NN: class template "list" is
>not compatible with template template parameter "Cont"
>class  IntContainer : public TemplateContainer< int, list > {};
>                                                     ^

That is a precise description of the problem. I haven't heard of
Cantata++, but it seems to have good template syntax support.

>The people at IPL mean, that such an example is illegal C++.

And they are right.

>But Stroustrup has described such "template template parameter"
>construct in his book "The C++ Programming Language", chapter C.13.3
>"Templates as Template Parameter".

And he is right.

>
>So, tell me, please,
>whether the code is illegal (in which case Stroustrup and g++ are
>wrong),
>or it is legal (in which case the most C++ compilers are wrong)?

Neither is quite true. Stroustrup and IPL are right, g++ is wrong.
MSVC6 doesn't support template template parameters at all, and so it
is wrong for a different reason.

Tom



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Michiel.Salters@cmg.nl (Michiel Salters)
Date: 7 Feb 2002 00:57:00 GMT
Raw View
plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 1012998411 30360 127.0.0.1 (6 Feb 2002 12:26:51 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: 6 Feb 2002 12:26:51 GMT
Content-Length: 2076
ReSent-Date: Wed, 6 Feb 2002 08:26:50 -0800 (PST)
ReSent-From: Steve Clamage <clamage@eng.sun.com>
ReSent-To:  <c++-submit@netlab.cs.rpi.edu>
ReSent-Subject: Re: template template parameter - is it legal?
ReSent-Message-ID: <Pine.SOL.4.33.0202060826500.2343@taumet>

Oleg Petsa <oleg.petsa@alcatel.de> wrote in message
news:<3C5FDE90.E2C70EBA@alcatel.de>...
> Hello all!
>
> I have a problem with the so called "template template parameter".
> The following code is compiled by g++ without any errors and warnings
> (except for warnings for "use of old-style cast" in STL headers), but
> rejected by several other C++ compilers (MS Visual C++, Sun CC) and by
> IPL Cantata++ test tool:

Which versions of the "other" compilers ? I don't think it matters,
though.

> template< class T, template<class> class Cont >
> class TemplateContainer {
> protected:
>   Cont<T> _cont;
[SNIP]
> };
>
> #include <list>
> using std::list;
>
> class  IntContainer : public TemplateContainer< int, list > {};
[SNIP]

> The error message looks like:
>
> Cantata++ CPPSM  v2.2-FR01315 (c) 2001 IPL Information Processing Ltd
> Error SM10019: "TemplContTest.cpp", line NN: class template "list" is
> not compatible with template template parameter "Cont"
> class  IntContainer : public TemplateContainer< int, list > {};

Well, list is a template taking 3+ parameters, but only the first
doesn't have a default. Accepting a N-parameter template with
defaults where a lower number is needed is an extension.

A possible trick is to provide a one-parameter wrapper:

template < typename T >
class List : public std::list<T>;

This should be accepted. List now takes a single parameter.

BTW, this shows that we should (in C++0x) treat default parameters
as a shortcut to provide a number of overloads, which really are
distinct functions/templates. I.e. a function with N parameters and
M defaults is really a set of M+1 functions, with N-M to N
parameters.

> The people at IPL mean, that such an example is illegal C++.
> But Stroustrup has described such "template template parameter"
> construct in his book "The C++ Programming Language", chapter C.13.3
> "Templates as Template Parameter".

It's not the construct that's illegal. It's a parameter mismatch.
According to TC++PL and the standard, it doesn't have to be accepted.

HTH,
--
Michiel Salters



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Anthony Williams"<anthwil@nortelnetworks.com>
Date: 7 Feb 2002 00:56:59 GMT
Raw View
"Oleg Petsa" <oleg.petsa@alcatel.de> wrote in message
news:3C5FDE90.E2C70EBA@alcatel.de...
> I have a problem with the so called "template template parameter".
> The following code is compiled by g++ without any errors and warnings
> (except for warnings for "use of old-style cast" in STL headers), but
> rejected by several other C++ compilers (MS Visual C++, Sun CC) and by
> IPL Cantata++ test tool:

MSVC++ and Sun CC don't support template template parameters at all.

> template< class T, template<class> class Cont >
> class TemplateContainer {
[snipped class contents]
> };

> #include <list>
> using std::list;

> class  IntContainer : public TemplateContainer< int, list > {};

[snipped main]

> The error message looks like:

> Cantata++ CPPSM  v2.2-FR01315 (c) 2001 IPL Information Processing Ltd
> Error SM10019: "TemplContTest.cpp", line NN: class template "list" is
> not compatible with template template parameter "Cont"
> class  IntContainer : public TemplateContainer< int, list > {};
>                                                      ^

> The people at IPL mean, that such an example is illegal C++.
> But Stroustrup has described such "template template parameter"
> construct in his book "The C++ Programming Language", chapter C.13.3
> "Templates as Template Parameter".

> So, tell me, please,
> whether the code is illegal (in which case Stroustrup and g++ are
> wrong),
> or it is legal (in which case the most C++ compilers are wrong)?

Template template parameters are legal. However, the template parameters of
your template-template-parameters must exactly match the template passed in
(!). In this case, your template template parameter has one parameter, but
std::list has two (element type + allocator), so it is "not compatible".

Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optical Components Ltd
The opinions expressed in this message are not necessarily those of my
employer





      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Philippe A. Bouchard" <philippeb@videotron.ca>
Date: 7 Feb 2002 00:57:03 GMT
Raw View
It is perfectly legal but try it with your own template class to see if the
problem is not default arguments...

template <typename _T> class list_test
{
};

[...]

class  IntContainer : public TemplateContainer< int, list_test > {};

[...]

"Oleg Petsa" <oleg.petsa@alcatel.de> wrote in message
news:3C5FDE90.E2C70EBA@alcatel.de...
>
> Hello all!
>
> I have a problem with the so called "template template parameter".
> The following code is compiled by g++ without any errors and warnings
> (except for warnings for "use of old-style cast" in STL headers), but
> rejected by several other C++ compilers (MS Visual C++, Sun CC) and by
> IPL Cantata++ test tool:
>
>
> template< class T, template<class> class Cont >
> class TemplateContainer {
> protected:
>   Cont<T> _cont;
> public:
>   typedef typename Cont<T>::iterator Iterator;
>
>   Iterator begin() { return _cont.begin(); };
>   void pushBack(const T& t) { _cont.push_back(t); };
>   //...
>   //...
> };
>
> #include <list>
> using std::list;
>
> class  IntContainer : public TemplateContainer< int, list > {};
>
> int main(void)
> {
>   IntContainer  intContainer;
>
>   intContainer.pushBack(20);
>   //...
> };
>
>
> The error message looks like:
>
> Cantata++ CPPSM  v2.2-FR01315 (c) 2001 IPL Information Processing Ltd
> Error SM10019: "TemplContTest.cpp", line NN: class template "list" is
> not compatible with template template parameter "Cont"
> class  IntContainer : public TemplateContainer< int, list > {};
>                                                      ^
>
>
> The people at IPL mean, that such an example is illegal C++.
> But Stroustrup has described such "template template parameter"
> construct in his book "The C++ Programming Language", chapter C.13.3
> "Templates as Template Parameter".
>
> So, tell me, please,
> whether the code is illegal (in which case Stroustrup and g++ are
> wrong),
> or it is legal (in which case the most C++ compilers are wrong)?

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do 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.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: Oleg Petsa <oleg.petsa@alcatel.de>
Date: Thu, 7 Feb 2002 15:31:34 GMT
Raw View
> >template< class T, template<class> class Cont >
>
> Cont must be a template that takes one parameter.
>
> >class TemplateContainer
> ....
> >class  IntContainer : public TemplateContainer< int, list > {};
>
> std::list is a template that takes two parameters, one of which
> happens to have a default value. The default parameter is irrelevent
> when it comes to template template parameters, although some have
> argued against this. Hence the above is an error. Also, why use
> derivation when you meant a typedef?
>

typedef results in the same error message. Really, it's irrelevant here,
whether typedef or derivation. The problem is template with default
arguments.

> >The error message looks like:
> >
> >Cantata++ CPPSM  v2.2-FR01315 (c) 2001 IPL Information Processing Ltd
> >Error SM10019: "TemplContTest.cpp", line NN: class template "list" is
> >not compatible with template template parameter "Cont"
> >class  IntContainer : public TemplateContainer< int, list > {};
> >                                                     ^
>
> That is a precise description of the problem. I haven't heard of
> Cantata++, but it seems to have good template syntax support.
>
> >The people at IPL mean, that such an example is illegal C++.
>
> And they are right.
>
> >But Stroustrup has described such "template template parameter"
> >construct in his book "The C++ Programming Language", chapter C.13.3
> >"Templates as Template Parameter".
>
> And he is right.

Excuse me, but Stroustrup and IPL cannot be both right. Have you read
the chapter C.13.3 of 3rd Edition of "The C++ Programming Language"? If
yes, you will notice, that Stroustrup's construct and use of it are the
same as in my example. He declares formal parameter "Cont" as template
with 1 unnamed parameter, and supplies factual parameter "list", which
has 2 template parameters, but the 2nd param has default value (so that
"list" can be instantiated with 1 parameter).

> Neither is quite true. Stroustrup and IPL are right, g++ is wrong.

Again, Stroustrup and g++ mean the same, they should be both right or
both wrong.

---
[ 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.research.att.com/~austern/csc/faq.html                ]