473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Are default function parameters in allowed in function templates?

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
11 1958
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Stembera | last post by:
I would like to use default parameters in nested templates but MS VC++ 7.1 chokes on it. Does anyone know how to fix the simple example below or if indeed it is possible? template <int N=7>...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
8
by: cody | last post by:
Why doesn't C# allow default parameters for methods? An argument against I hear often is that the default parameters would have to be hardbaken into the assembly, but why? The Jit can take care of...
4
by: sods | last post by:
Hi, I write a test code about template used for strategy. it's very similar to sample code in TC++PL 13.4.1. #include <iostream> #include <string> using std::basic_string;
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
12
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.