473,830 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2002
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.or g> 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.or g> 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******@kanga roologic.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.or g> wrote:
------------------
template<cla ss T, class C> bool count(T x[], C cmp = std::less<T>())
...
int x[10]
count(x);


i would try this:

template<clas s 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.or g> 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.or g> 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4785
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> class A { };
16
16300
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
3047
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 this, if the code is jitted the "push xyz" instructions of the actual default values can be inserted. To make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself why one to...
4
7245
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
2767
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 no errors compiling this:
12
2332
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 <iostream> using namespace std; template <class ClassB> class ClassA
74
16051
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 creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
8
284
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
6654
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
9791
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10771
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10487
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10525
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9313
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7745
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3076
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.