Topic: Default Arguments for Template functions


Author: maisonave@axter.com (foo)
Date: Sat, 29 Dec 2001 03:46:06 CST
Raw View
template<class T1, class T2>
void foo(T1 Src1, T2 Src2 = 0)
{
}

int main(int, char*)
{
 foo(1,2); //OK (compiles)
 foo(1); //Fails to compiles
 return 0;
}

In the above code, even though the foo function has a default argument
for the second variable, it still fails to compile when only one
variable is pass to the function.

What type of problem would be intruduced if the standards were changed
to support default arguments for template functions?

---
[ 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: "Roger Orr" <rogero@howzatt.demon.co.uk>
Date: Sat, 29 Dec 2001 08:52:33 CST
Raw View
foo wrote in message ...
>template<class T1, class T2>
>void foo(T1 Src1, T2 Src2 = 0)
>{
>}
>
>int main(int, char*)
>{
> foo(1,2); //OK (compiles)
> foo(1); //Fails to compiles
> return 0;
>}
>
>In the above code, even though the foo function has a default argument
>for the second variable, it still fails to compile when only one
>variable is pass to the function.
>
>What type of problem would be intruduced if the standards were changed
>to support default arguments for template functions?

I'm not quite sure what you're asking...there are two defaults involved
here!

You code won't compile because the compiler can't deduce the type of T2.
You can solve it by:-

  foo<int,int>(1);

or by using T1 for _both_ function arguments:-

  template<class T1>
  void foo(T1 Src1, T1 Src2 = 0)
  {
  }

or by adding an overload:-

  template<class T1>
  void foo( T1 Src1 )
  {
    return foo( Src1, 0 );
  }

The only thing you _can't_ do is:-

  template<class T1, class T2 = T1> // won't compile...
  void foo( ... )

The reason given is usually that you need default template arguments for
class templates since you usually explicitly create them, but you don't need
them for function templates since the argument types imply the template
arguments.

I suspect it would be possible, but complicated, to enhance template
functions to allow default template arguments but since you can do the same
thing already using function overloading it is, IMHO, unlikely to happen.

Hope this helps,
Roger Orr
--
MVP in C++ at www.brainbench.com



---
[ 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: "foofoo" <foo@axter.com>
Date: Sat, 29 Dec 2001 16:58:59 CST
Raw View
"Roger Orr" <rogero@howzatt.demon.co.uk> wrote in message
news:1009630679.15469.0.nnrp-14.9e98aa01@news.demon.co.uk...
> foo wrote in message ...
> >template<class T1, class T2>
> >void foo(T1 Src1, T2 Src2 = 0)
> >{
> >}
> >
> >int main(int, char*)
> >{
> > foo(1,2); //OK (compiles)
> > foo(1); //Fails to compiles
> > return 0;
> >}
> >
> >In the above code, even though the foo function has a default argument
> >for the second variable, it still fails to compile when only one
> >variable is pass to the function.
> >
> >What type of problem would be intruduced if the standards were changed
> >to support default arguments for template functions?
>
> I'm not quite sure what you're asking...there are two defaults involved
> here!
>
> You code won't compile because the compiler can't deduce the type of T2.
> You can solve it by:-
>
>   foo<int,int>(1);
>
> or by using T1 for _both_ function arguments:-
>
>   template<class T1>
>   void foo(T1 Src1, T1 Src2 = 0)
>   {
>   }
>
> or by adding an overload:-
>
>   template<class T1>
>   void foo( T1 Src1 )
>   {
>     return foo( Src1, 0 );
>   }
>
> The only thing you _can't_ do is:-
>
>   template<class T1, class T2 = T1> // won't compile...
>   void foo( ... )
>
> The reason given is usually that you need default template arguments for
> class templates since you usually explicitly create them, but you don't
need
> them for function templates since the argument types imply the template
> arguments.

That is exactly my point.
You can give default template type to a class, but you can not do this for a
template function.
Example:
template<class T1, class T2 = int> //int is ignored here
void foo(T1 Src1, T2 Src2 = 0){}//0 is ignored here

IMHO: Template functions should have all the c++ functionality that a
non-template function has.  Because the above code does not work, I feel
that an important C++ feature does not exist in template functions.

Although overloading the function can do the job, it is not an adequate
replacement for default argument(s) function.
If the original designers of the language thought that default arguments
were not that important, (because of function overloading), I'm sure it
would not have been added to the language.

I think this is an important feature that should be added to template
functions, to make them more compatible to non-template functions.

What I'm really interested, is to get feedback on what types of problems
would be introduced if this feature was added to template-functions.

---
[ 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: comeau@panix.com (Greg Comeau)
Date: Sun, 30 Dec 2001 04:13:53 CST
Raw View
In article <1009630679.15469.0.nnrp-14.9e98aa01@news.demon.co.uk>,
Roger Orr <rogero@howzatt.demon.co.uk> wrote:
>The only thing you _can't_ do is:-
>
>  template<class T1, class T2 = T1> // won't compile...
>  void foo( ... )
>
>The reason given is usually that you need default template arguments for
>class templates since you usually explicitly create them, but you don't need
>them for function templates since the argument types imply the template
>arguments.
>
>I suspect it would be possible, but complicated, to enhance template
>functions to allow default template arguments but since you can do the same
>thing already using function overloading it is, IMHO, unlikely to happen.

Well, it is under review as a possible defect.
--
Greg Comeau         Watch for 'export'!
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?

---
[ 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: "Ken Alverson" <Ken@Alverson.com>
Date: Sun, 30 Dec 2001 20:37:54 CST
Raw View
"foofoo" <foo@axter.com> wrote in message
news:VRlX7.337746$5A3.129936574@news1.rdc2.pa.home.com...
>
> Although overloading the function can do the job, it is not an adequate
> replacement for default argument(s) function.
> If the original designers of the language thought that default arguments
> were not that important, (because of function overloading), I'm sure it
> would not have been added to the language.

That's not really a valid argument, because default arguments preceeded
function overloading in the design process.  If function overloading was
already available, default arguments might never have been added.

Ken


---
[ 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                ]