By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,563 Members | 1,280 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,563 IT Pros & Developers. It's quick & easy.

Are default function parameters in allowed in function templates?

P: n/a
BRG
I know that default template arguments cannot be used in function
templates but are default function parameters legal?

That is, is this:
----------------------------------
#include <functional>

template<class T, class C> bool count(T x[], C cmp = std::less<T>())
{
for(int i = 0; i < 9; ++i)
if(cmp([i], x[i + 1]))
return true;
return false;
}
----------------------------------
legal C++ code?

count(x, std::less<int>()); // compiles ok, but
count(x); // gives "could not deduce template argument for 'C'"

Brian Gladman
Jul 22 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
BRG
BRG wrote:
I know that default template arguments cannot be used in function
templates but are default function parameters legal?

That is, is this:
----------------------------------
#include <functional>

template<class T, class C> bool count(T x[], C cmp = std::less<T>())
{
for(int i = 0; i < 9; ++i)
if(cmp([i], x[i + 1]))
return true;
return false;
}
----------------------------------
legal C++ code?
int x[10] // - I should have added this line as well count(x, std::less<int>()); // compiles ok, but
count(x); // gives "could not deduce template argument for 'C'"


Brian Gladman
Jul 22 '05 #2

P: n/a
BRG wrote:
I know that default template arguments cannot be used in function
templates but are default function parameters legal?
...


They are legal, but they won't work the way you expect them to work.
Default arguments for function parameters are not used in template
argument deduction process.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #3

P: n/a
BRG
Andrey Tarasevich wrote:
BRG wrote:
I know that default template arguments cannot be used in function
templates but are default function parameters legal?
...


They are legal, but they won't work the way you expect them to work.
Default arguments for function parameters are not used in template
argument deduction process


Thanks for the explanation - but I am still puzzled. In:

------------------
template<class T, class C> bool count(T x[], C cmp = std::less<T>())
....
int x[10]
count(x);
....
------------------

the template argument T can be deduced to be 'int' from the first
function parameter x. I hence don't see why deduction is involved in
the second function parameter since this seems to be substitution (T =>
int) rather than a deduction.

I guess this is a quirk of C++. But it is a nuisance since I have to
work around it.

Thanks again for your help.

Brian Gladman
Jul 22 '05 #4

P: n/a
On Tue, 04 Jan 2005 00:17:50 +0000, BRG <br*@nowhere.org> wrote:
------------------
template<class T, class C> bool count(T x[], C cmp = std::less<T>())
...
int x[10]
count(x);


i would try this:

template<class T, class C = std::less<T> > bool count(T x[], C cmp = C());

(template parameters can have default values (i.e. data types), and you
avoid the possible type mismatch between the actual class passed in for
template parameter C and the class std::less<T>)
Jul 22 '05 #5

P: n/a
Ulrich Achleitner wrote:
On Tue, 04 Jan 2005 00:17:50 +0000, BRG <br*@nowhere.org> wrote:
------------------
template<class T, class C> bool count(T x[], C cmp = std::less<T>())
...
int x[10]
count(x);


i would try this:

template<class T, class C = std::less<T> > bool count(T x[], C cmp =
C());

(template parameters can have default values (i.e. data types), and
you avoid the possible type mismatch between the actual class passed
in for template parameter C and the class std::less<T>)


Actually, function template parameters currently cannot have defaults. This
should be corrected in the next version of the standard. See
http://www.open-std.org/jtc1/sc22/wg...fects.html#226.

Jonathan
Jul 22 '05 #6

P: n/a
On Tue, 4 Jan 2005 02:24:13 -0700, Jonathan Turkanis
<te******@kangaroologic.com> wrote:
Actually, function template parameters currently cannot have defaults.
This
should be corrected in the next version of the standard. See
http://www.open-std.org/jtc1/sc22/wg...fects.html#226.


thanks for the hint and the link :)
ulrich
Jul 22 '05 #7

P: n/a
BRG
Jonathan Turkanis wrote:
Ulrich Achleitner wrote:
On Tue, 04 Jan 2005 00:17:50 +0000, BRG <br*@nowhere.org> wrote:
------------------
template<class T, class C> bool count(T x[], C cmp = std::less<T>())
...
int x[10]
count(x);


i would try this:

template<class T, class C = std::less<T> > bool count(T x[], C cmp =
C());

(template parameters can have default values (i.e. data types), and
you avoid the possible type mismatch between the actual class passed
in for template parameter C and the class std::less<T>)


Actually, function template parameters currently cannot have defaults. This
should be corrected in the next version of the standard. See
http://www.open-std.org/jtc1/sc22/wg...fects.html#226.


Yes, that was my understanding too, which is why I was trying the other
approach.

Brian Gladman
Jul 22 '05 #8

P: n/a
On Tue, 04 Jan 2005 10:51:15 +0000, BRG <br*@nowhere.org> wrote:
template<class T, class C> bool count(T x[], C cmp = std::less<T>())
this is never a correct place for a default _type_. in a function's
parameter list there can only be default _values_.
i would try this:

template<class T, class C = std::less<T> > bool count(T x[], C cmp =
C()); Actually, function template parameters currently cannot have defaults.


so, a valid version would be:
template<class T, class C> bool count(T x[], C cmp = C());
Yes, that was my understanding too, which is why I was trying the other
approach.


more you will not get with a template function, it seems.
Jul 22 '05 #9

P: n/a
BRG
Ulrich Achleitner wrote:
On Tue, 04 Jan 2005 10:51:15 +0000, BRG <br*@nowhere.org> wrote:
> template<class T, class C> bool count(T x[], C cmp = std::less<T>())
this is never a correct place for a default _type_. in a function's
parameter list there can only be default _values_.


Yes, but std::less<T> and std::less<T>() are values, not types, aren't they?

I thought that these are actual instances (or components) of a type (a
structure) defined in <functional>.
i would try this:

template<class T, class C = std::less<T> > bool count(T x[], C cmp =
C());

Actually, function template parameters currently cannot have defaults.
so, a valid version would be:
template<class T, class C> bool count(T x[], C cmp = C());


I'll try this, thanks.
Yes, that was my understanding too, which is why I was trying the
other approach.


more you will not get with a template function, it seems.


At least until the new standard arrives :-)

Brian Gladman
Jul 22 '05 #10

P: n/a
On Tue, 04 Jan 2005 13:06:28 +0000, BRG <br*@nowhere.org> wrote:
>> template<class T, class C> bool count(T x[], C cmp = std::less<T>())

this is never a correct place for a default _type_. in a function's
parameter list there can only be default _values_.


Yes, but std::less<T> and std::less<T>() are values, not types, aren't
they?


the former is a type, the latter is a value / object. sorry, i overlooked
the () in your original posting...
Jul 22 '05 #11

P: n/a
BRG
Ulrich Achleitner wrote:
On Tue, 04 Jan 2005 13:06:28 +0000, BRG <br*@nowhere.org> wrote:
>>> template<class T, class C> bool count(T x[], C cmp = std::less<T>())

this is never a correct place for a default _type_. in a
function's parameter list there can only be default _values_.

Yes, but std::less<T> and std::less<T>() are values, not types,
aren't they?


the former is a type, the latter is a value / object. sorry, i
overlooked the () in your original posting...


Thanks for taking an interest in this.

I have had to abandon defaults here as I cannot find a way of making
them work. What has worked as a way of inling the comparison code (to
avoid the overhead of a function call) is:

template<class T> inline bool
lt_f(const T& x, const T& y) { return x < y; }

template<class T, bool C(const T&, const T&)>
bool count(T x[]) { ... C(x[j], x[i]) ... }

...
int q[10];
count<int, lt_f<int> >(q);
...

But I have had to build my own comparison templates in place of those in
<functional>.

Thanks again for your input.

Brian Gladman
Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.