Are default function parameters in allowed in function templates?

B

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(, 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
 
B

BRG

BRG said:
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(, x[i + 1]))
return true;
return false;
}

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
 
A

Andrey Tarasevich

BRG said:
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.
 
B

BRG

Andrey said:
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
 
U

Ulrich Achleitner

------------------
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>)
 
J

Jonathan Turkanis

Ulrich said:
------------------
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/wg21/docs/cwg_defects.html#226.

Jonathan
 
B

BRG

Jonathan said:
Ulrich said:
------------------
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/wg21/docs/cwg_defects.html#226.

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

Brian Gladman
 
U

Ulrich Achleitner

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 said:
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.
 
B

BRG

Ulrich said:
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 said:
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.
more you will not get with a template function, it seems.

At least until the new standard arrives :)

Brian Gladman
 
U

Ulrich Achleitner

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...
 
B

BRG

Ulrich said:
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) ... }

...
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top