473,940 Members | 6,003 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
11 2009
On Tue, 04 Jan 2005 13:06:28 +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?


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


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
4788
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
16340
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
3055
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
7252
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
2777
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
2344
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
16105
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
6670
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
9963
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
11110
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...
0
10659
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9858
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
8218
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
6077
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.