Topic: Q: templates and default argument syntax


Author: "Victor Bazarov" <v_bazarov@j_ps.net>
Date: 1998/10/16
Raw View
James Russell Kuyper Jr. wrote in message <362639E5.3E377E96@wizard.net>...
>
>Biju Thomas wrote:
>[Re: prohibition on default template arguments on function templates,
>and the use of such argument in section 13.4.1 of Stroustrup's book]
>> Why was it changed between the time Stroustrup published his book and
>> the Standard was approved?
>
>Because he chose to publish his book before the standardization process
>was completed.


Sorry, wrong answer. Would you like to try once more?
The question was: "Why was it changed?", not "Why do the book and
the Standard differ?". As I understand it Biju wanted to know
the reasoning behind the change in the Standard. So would I.

Care to comment?

Thanks.

Victor
--
Please remove underscores from my address when replying by mail


      [ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: bs@research.att.com (Bjarne Stroustrup)
Date: 1998/10/17
Raw View
"James Russell Kuyper Jr." <kuyper@wizard.net> writes:

 > Biju Thomas wrote:
 > [Re: prohibition on default template arguments on function templates,
 > and the use of such argument in section 13.4.1 of Stroustrup's book]
 > > Why was it changed between the time Stroustrup published his book and
 > > the Standard was approved?
 >
 > Because he chose to publish his book before the standardization process
 > was completed.

No. That's not the reason. There was no change to the standard text relating
to this between the publication of "The C++ Programming Language" and the
final standard.

The prohibition was in the draft standard text for years. It is a
leftover from the time before non-type template arguments were allowed.
I failed to notice the sentence prohibiting default arguments for function
templates - or it would almost certainly have been voted out of the text.
We had used examples involving default arguments for function templates
in discussions in the committee without niticing that they were prohibited.

I have added the issue to the committees list of potential defects to
consider. I expect that restriction to be lifted eventually.

 - Bjarne
Bjarne Stroustrup - http://www.research.att.com/~bs
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Vanja Cvjetko <vcvjetko@linux.rulz.tel.hr>
Date: 1998/10/11
Raw View
/*
    newsgroups: comp.lang.c++,
                 comp.lang.c++.moderated,
                comp.std.c++

    reply to  : vcvjetko@linux.rulz.zg.tel.hr
                by removing praising the best OS


   Hi there!

   I have some, probably stupid, questions regarding template
   classes and functions with default arguments and non template
classes.

   Suppose we have one template function foo and template class Bar
like:
 */

# include <iostream.h>

template <class Type=int>
Type foo(Type t)
{
 return t;
};

template <class Type=int>
class Bar
{
 Type t_;
 public:
  void output();

};


/*

 */
int main()
{
 /*
    CASE 1:
  */
  cout << foo<double>(1.000) << endl;

 /*
    CASE 2: function temlpate default arguments used.  **note with and
without <>**
  */
  cout << foo(1)             << endl;
  cout << foo<>(1)           << endl;


 /*
    CASE 3:
  */
 Bar<double> bd_;
 Bar<int>    bi_1_;


 /*
    CASE 3:
  */
 Bar<>    bi_2_;


 /*
    CASE 4:
  */
 Bar      bi_3_;

 /*
   COMPILE OUTPUTS:

 1. egcs-1.0.1

    g++ egcs-1.0.1  **note without <>** is not accepted
    doesn't compile complains:
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:
In function `int main()':
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:73:
`Bar' undeclared (first use this function)
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:73:
(Each undeclared identifier is reported only once
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:73:
for each function it appears in.)
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:73:
parse error before `;'


 2. gcc-2.8.1

Reading specs from /usr/lib/gcc-lib/i586-pc-linux-gnulibc1/2.8.1/specs
gcc version 2.8.1
 /usr/lib/gcc-lib/i586-pc-linux-gnulibc1/2.8.1/cpp -lang-c++ -v -I
/home/gcl/gcl/include -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus
-D__GNUC_MINOR__=8 -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__
-D__linux__ -D__unix -D__linux -Asystem(posix) -D__EXCEPTIONS -g -Wall
-Di386 -Di586 -Asystem(unix) -Acpu(i386) -Amachine(i386) -D__i386__
-D__i586__ -Asystem(unix) -Acpu(i386) -Amachine(i386)
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp
/tmp/cca22043.ii
GNU CPP version 2.8.1 (i386 GNU/Linux with ELF)
#include "..." search starts here:
#include <...> search starts here:
 /home/gcl/gcl/include
 /usr/include/g++
 /usr/lib/g++-include
 /usr/local/include
 /usr/i586-pc-linux-gnulibc1/include
 /usr/lib/gcc-lib/i586-pc-linux-gnulibc1/2.8.1/include
 /usr/include
End of search list.
 /usr/lib/gcc-lib/i586-pc-linux-gnulibc1/2.8.1/cc1plus /tmp/cca22043.ii
-quiet -dumpbase
template_with_default_arguments_and_nontemplate_classes.cc -g -Wall
-version -ftemplate-depth-45 -o /tmp/cca22043.s
GNU C++ version 2.8.1 (i586-pc-linux-gnulibc1) compiled by GNU C version
2.8.1.
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:
In function `int main()':
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:73:
`Bar' undeclared (first use this function)
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:73:
(Each undeclared identifier is reported only once
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:73:
for each function it appears in.)
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:73:
parse error before `;'


 3. egcs-1.1b

Reading specs from
/usr/egcs/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.91.57/specs
gcc version egcs-2.91.57 19980901 (egcs-1.1 release)
 /usr/egcs/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.91.57/cpp -lang-c++
-v -I /home/gcl/gcl/include -undef -D__GNUC__=2 -D__GNUG__=2
-D__cplusplus -D__GNUC_MINOR__=91 -D__ELF__ -Dunix -Dlinux -D__ELF__
-D__unix__ -D__linux__ -D__unix -D__linux -Asystem(posix) -D__EXCEPTIONS
-g -Wall -Asystem(unix) -Acpu(i386) -Amachine(i386) -Di386 -D__i386
-D__i386__ -Di586 -Dpentium -D__i586 -D__i586__ -D__pentium
-D__pentium__
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp
/tmp/ccScdGfY.ii
GNU CPP version egcs-2.91.57 19980901 (egcs-1.1 release) (i386
Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
 /home/gcl/gcl/include
 /usr/egcs/include/g++
 /usr/local/include
 /usr/egcs/i586-pc-linux-gnulibc1/include
 /usr/egcs/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.91.57/include
 /usr/include
End of search list.
 /usr/egcs/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.91.57/cc1plus
/tmp/ccScdGfY.ii -quiet -dumpbase
template_with_default_arguments_and_nontemplate_classes.cc -g -Wall
-version -ftemplate-depth-45 -o /tmp/cc39lA36.s
GNU C++ version egcs-2.91.57 19980901 (egcs-1.1 release)
(i586-pc-linux-gnulibc1) compiled by GNU C version egcs-2.91.57 19980901
(egcs-1.1 release).
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:
In function `int main()':
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:73:
`Bar' undeclared (first use this function)
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:73:
(Each undeclared identifier is reported only once
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:73:
for each function it appears in.)
/home/gcl/gcl/example/Tests/template_with_default_arguments_and_nontemplate_classes.cpp:73:
parse error before `;'



  */

 return 0;
}


/*
  Question:

  Why doesn't compiler accept
                            Bar and Bar<>
  while it accepts
                            foo and foo<> ????



  Is difference between template class inst. with default args
                            Bar and Bar<>
  opposite to template fnc with default args
                            foo and foo<>
  made on purpose or...?




  If there is difference between
                           Bar and Bar<> ?

  Because if there **exists** difference then one should be able to
  declare and define the **nontemplate** class with the same name.




  And if there should be difference between
                           Bar and Bar<>
  why doesn't
                           foo and foo<>
  differ and why compiler doesn't complain??




  One more question for our future plans:
  Which one form is or will be according to ANSI C++ and if both are OK
  which one should we prefer??
                           Bar or Bar<>
   and
                           foo or foo<>   ????



  Is it an bug or feature in g++, or am I misunderstanding the templates
and
  arguments?

  Pls help and thanx in advance.

  miljenko
 */




      [ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1998/10/12
Raw View
On 11 Oct 98 10:57:55 GMT, Vanja Cvjetko <vcvjetko@linux.rulz.tel.hr> wrote:

>template <class Type=int>
>Type foo(Type t)
>{
> return t;
>};

Default template arguments are not allowed for template functions.
This is because default template arguments may interfere with template
argument deduction.  That is, when calling a template function, you
almost never have to supply the <...> list because the compiler will
figure it out!  By contrast, for classes, you always have to supply
the <...> list.  Eg,

foo(3); // since '3' is an int, this calls foo<int>(3)
foo(3.0); // since '3.0' is a double, this calls foo<double>(3.0)

Default arguments are useless in most cases.  In cases where you must
supply the <...> list, eg. for a template func that takes no args,
default args would be useful.  But that's a rare case.

Besides, template default args should rarely be used.


>template <class Type=int>
>class Bar
>{
> Type t_;
> public:
>  void output();
>
>};

OK.  But no ctor?



>  If there is difference between
>                           Bar and Bar<> ?

You must use "Bar<> b", not "Bar b".  This is stated somewhere in the
standard!


--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Biju Thomas <bijuthom@ibm.net>
Date: 1998/10/12
Raw View
Siemel Naran wrote:
>
> On 11 Oct 98 10:57:55 GMT, Vanja Cvjetko <vcvjetko@linux.rulz.tel.hr> wrote:
>
> >template <class Type=int>
> >Type foo(Type t)
> >{
> > return t;
> >};
>
> Default template arguments are not allowed for template functions.

Really? Where is this specified in the standard?

AFAIK, there is no such restriction. See Stroustrup 13.4.1 where he uses
such an example.

> This is because default template arguments may interfere with template
> argument deduction.  That is, when calling a template function, you
> almost never have to supply the <...> list because the compiler will
> figure it out!  By contrast, for classes, you always have to supply
> the <...> list.  Eg,
>
> foo(3); // since '3' is an int, this calls foo<int>(3)
> foo(3.0); // since '3.0' is a double, this calls foo<double>(3.0)
>
> Default arguments are useless in most cases.

Not true at all. Take a look at the standard library implementation and
you will see it used almost anywhere. They are useful to specify a
default policy (for example, allocators) which can be overridden by
clients.

Regards,
Biju Thomas
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: AllanW@my-dejanews.com
Date: 1998/10/13
Raw View

> > On 11 Oct 98 10:57:55 GMT,
> > Vanja Cvjetko <vcvjetko@linux.rulz.tel.hr> wrote:
> > >template <class Type=int>
> > >Type foo(Type t)
> > >{
> > > return t;
> > >};

> Siemel Naran wrote:
> > Default template arguments are not allowed for template functions.

In article <36220449.5F582212@ibm.net>,
  Biju Thomas <bijuthom@ibm.net> wrote:

> Really? Where is this specified in the standard?

> AFAIK, there is no such restriction. See Stroustrup 13.4.1 where he uses
> such an example.

  14.1  Template parameters                                 [temp.param]

8 ...                           A default template-argument shall not be
  specified  in  a  function template declaration or a function template
  definition.  ...

> > This is because default template arguments may interfere with template
> > argument deduction.  That is, when calling a template function, you
> > almost never have to supply the <...> list because the compiler will
> > figure it out!  By contrast, for classes, you always have to supply
> > the <...> list.  Eg,
> >
> > foo(3); // since '3' is an int, this calls foo<int>(3)
> > foo(3.0); // since '3.0' is a double, this calls foo<double>(3.0)
> >
> > Default arguments are useless in most cases.

> Not true at all. Take a look at the standard library implementation and
> you will see it used almost anywhere. They are useful to specify a
> default policy (for example, allocators) which can be overridden by
> clients.

Please tell us which one(s).

Container classes have default template arguments, and some of their
member functions have default function arguments. But none of them
that I know of have template member functions which accept default
template arguments, nor is there any template global function which
has default template arguments.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own

      [ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1998/10/14
Raw View
On 12 Oct 1998 21:08:02 -0400, Biju Thomas <bijuthom@ibm.net> wrote:
>Siemel Naran wrote:

>> Default template arguments are not allowed for template functions.

>Really? Where is this specified in the standard?

[14.1 Template parameters]

9  A default template-argument is a template-argument (14.3) specified
   after the = in a template-parameter.  A default template-argument
   may be specified for any kind of template-parameter (type, non-type,
   template).  A default template-argument may be specified in a class
   template declaration or a class template definition.  A default
   template-argument shall not be specified in a function template
   declaration or a function template definition, nor in the template-
   parameter-list of the definition of a member of a class template.


The example in Stroustrup 13.4.1 seems to go against the passage quoted
above:

template <class T, class C=Cmp<T> >
int compare(const String<T>& str1, const String<T>& str2);


>> Default arguments are useless in most cases.

>Not true at all. Take a look at the standard library implementation and
>you will see it used almost anywhere. They are useful to specify a
>default policy (for example, allocators) which can be overridden by
>clients.

Sorry.  I meant default template arguments in functions are useless in
most cases.  This is because template argument deduction almost always
determines the template arguments.  For classes, on the other hand,
default template arguments are very useful.  And yes, the standard
library is dotted with them.  And default arguments for the parameter
list in functions are very useful too.


--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------

      [ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "James Russell Kuyper Jr." <kuyper@wizard.net>
Date: 1998/10/14
Raw View
Biju Thomas wrote:
>
> Siemel Naran wrote:
....
> > Default template arguments are not allowed for template functions.
>
> Really? Where is this specified in the standard?

Section 14.1 "Template Parameters" paragraph 9:

"A default template-argument shall not be specified in a function
template declaration or a function template definition, nor in the
template-parameter-list of the definition of a member of a class
template."

      [ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Biju Thomas <bijuthom@ibm.net>
Date: 1998/10/15
Raw View
AllanW@my-dejanews.com wrote:
>
> In article <36220449.5F582212@ibm.net>,
>   Biju Thomas <bijuthom@ibm.net> wrote:
> > Siemel Naran wrote:
> > > Default template arguments are not allowed for template functions.
> > > Default arguments are useless in most cases.
>
> > Not true at all. Take a look at the standard library implementation and
> > you will see it used almost anywhere. They are useful to specify a
> > default policy (for example, allocators) which can be overridden by
> > clients.
>
> Please tell us which one(s).
>
> Container classes have default template arguments, and some of their
> member functions have default function arguments. But none of them
> that I know of have template member functions which accept default
> template arguments, nor is there any template global function which
> has default template arguments.
>

I was referring to Siemel's comment that 'default arguments are useless
in most cases', not specifically about default template arguments in
template functions.

Since the standard disallows it, it should not be there in any library
:-)

Thanks guys, for clarifying it.

Why was it changed between the time Stroustrup published his book and
the Standard was approved?

Regards,
Biju Thomas

      [ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "James Russell Kuyper Jr." <kuyper@wizard.net>
Date: 1998/10/15
Raw View
Biju Thomas wrote:
[Re: prohibition on default template arguments on function templates,
and the use of such argument in section 13.4.1 of Stroustrup's book]
> Why was it changed between the time Stroustrup published his book and
> the Standard was approved?

Because he chose to publish his book before the standardization process
was completed.


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]