473,763 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help Please: Making a function template accept a default argument

Can anyone explain how I can make the following function accept an
default arguement for the last parameter, which should be an optional
functor?

template <typename T, typename FUNCTOR>
void bsort(T * si, T * ei, FUNCTOR cmpfunc)
{
int k = 0;
for (T * i = si; i < ei - 1; i++, k++)
for (T * j = si; j < (ei-k-1) ; j++)
if ( cmpfunc(*j, *(j+1)) )
swap(*j, *(j+1));
}

Let's say I want to make bsort accept either two or three arguements,
the last
one being optional one which determines the sorting order.
I have tested that overloading the template with another template like
this
template <typename T> void bsort(T *si, T * ei); works! But,

But I am wondering if I can achieve the same effect by providing the
default arguement rather than overloading?
For example, I could define a functor which can be used as the default
argument like below:

template <typename T>
class AscOrder {
public:
bool operator() (T a, T b)
{ return (a >= b ? true : false); }
};

I thought then I could use it as the default argument for the last
parameter like below:
template <typename T, typename FUNCTOR>
void bsort(T *si, T *ei, FUNCTOR cmpfunc = AscOrder<T>() );

But it doesn't work like I thought! The following function call
double dArray[] = {0.5,0.142,0.9, 1.5,2.23,2.19,3 .5,0.04};
bsort (dArray, dArray+8);
generates compiler error message saying "no matching function for
call..."

What I am misunderstandin g here? I would very appreciate if anyone
could kindly explain what I am doing wrong and how I might fix. I just
created this simple self-exercise to help me learn the features of
templates and functors, not to learn how to sort so please no advise
on sorting algorithms. TIA!
Jul 22 '05 #1
3 2136
"CoolPint" <co******@yahoo .co.uk> wrote...
Can anyone explain how I can make the following function accept an
default arguement for the last parameter, which should be an optional
functor?

template <typename T, typename FUNCTOR>
void bsort(T * si, T * ei, FUNCTOR cmpfunc)
{
int k = 0;
for (T * i = si; i < ei - 1; i++, k++)
for (T * j = si; j < (ei-k-1) ; j++)
if ( cmpfunc(*j, *(j+1)) )
swap(*j, *(j+1));
}

Let's say I want to make bsort accept either two or three arguements,
the last
one being optional one which determines the sorting order.
I have tested that overloading the template with another template like
this
template <typename T> void bsort(T *si, T * ei); works! But,

But I am wondering if I can achieve the same effect by providing the
default arguement rather than overloading?
For example, I could define a functor which can be used as the default
argument like below:

template <typename T>
class AscOrder {
public:
bool operator() (T a, T b)
{ return (a >= b ? true : false); }
};

I thought then I could use it as the default argument for the last
parameter like below:
template <typename T, typename FUNCTOR>
void bsort(T *si, T *ei, FUNCTOR cmpfunc = AscOrder<T>() );

But it doesn't work like I thought! The following function call
double dArray[] = {0.5,0.142,0.9, 1.5,2.23,2.19,3 .5,0.04};
bsort (dArray, dArray+8);
generates compiler error message saying "no matching function for
call..."
That's perfectly normal. Types (in your case 'FUNCTOR') cannot
be deduced from the default function arguments (14.8.2.4/17).
What I am misunderstandin g here? I would very appreciate if anyone
could kindly explain what I am doing wrong and how I might fix. I just
created this simple self-exercise to help me learn the features of
templates and functors, not to learn how to sort so please no advise
on sorting algorithms. TIA!


There is a simple fix. Provide another function (overloaded) with
only two arguments and call your function with what you want:

template<class T> void bsort(T t1, T t2) {
bsort(t1, t2, AscOrder<T>()); // calls the 3-argument variation
}

Victor
Jul 22 '05 #2
> That's perfectly normal. Types (in your case 'FUNCTOR') cannot
be deduced from the default function arguments (14.8.2.4/17).


Could you kindly elaborate a little more as to why Types cannot be
deduced
from the default function arguments? Is it because an instantion is
required
at the time of declaration to do so? Is it related to why we cannot
have default arguments for function template type parameters? I just
want to understand whys...

BTW, what's the number (14.8.2.4/17) in your answer? Is that a
chapter, sections of a book I should be looking at to get more
explanation on the issue? Well, I am relatively new to this group so
please bear with me.

Thanks again.
Jul 22 '05 #3
"CoolPint" <co******@yahoo .co.uk> wrote...
That's perfectly normal. Types (in your case 'FUNCTOR') cannot
be deduced from the default function arguments (14.8.2.4/17).
Could you kindly elaborate a little more as to why Types cannot be
deduced
from the default function arguments?


To be honest with you, I suspect that it's due to ambiguity. The
example in the Standard is similar to this:

template<class T> void foo(T t1 = 5, T t2 = 7)
{
}

int main()
{
foo(); // what's 'T'? 'int'? 'char'?
}

I suppose the Standard _allows_ compilers to avoid stretching too
much to try to figure it out because in some cases it's impossible,
so if only part of cases will be covered, the Standard cannot require
it.
Is it because an instantion is
required
at the time of declaration to do so? Is it related to why we cannot
have default arguments for function template type parameters? I just
want to understand whys...
For all "why"s, please ask in comp.std.c++. They talk C++ Standard.
We talk Standard C++. They hold the answers to "why". We try to
answer "how".

BTW, what's the number (14.8.2.4/17) in your answer? Is that a
chapter, sections of a book I should be looking at to get more
explanation on the issue? Well, I am relatively new to this group so
please bear with me.


It's the subsection and the paragraph in the Standard.

Good luck!

Victor
Jul 22 '05 #4

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

Similar topics

6
2647
by: Mike Alexeev | last post by:
What is the correct syntax for default values to member function templates? Here is my example: 1 struct A 2 { 3 typedef int typeA; 4 }; 5 6 struct B 7 {
4
1609
by: titancipher | last post by:
I have a container that I wish to allow the user to specify a custom comparison method very similar to std::less. However, I want it to function more like memcmp (returning -1 0 1), and I want to be able to vary the fields that are compared. The example below shows how I'd like it to fit together. struct fields { fields( int f1, int f2, int f3 ){ m_f = f1; m_f = f2; m_f = f3; }
3
2754
by: Capstar | last post by:
Hi NG, I am trying to get the attached piece of code to work, but I can't figure out what I'm doing wrong. To me it seems that when I don't pass an argument to x::do_something, it should use the default value, which is always_true(). but gcc says: no matching function for call to `x::do_something()' and msvs says: could not deduce template argument for '_Tp'
2
2508
by: xuatla | last post by:
The following is just a sample code to demostrate my question: ----------- template <typename T> class C { public: friend void f1(double i=2) { std::cout << i; } ; };
8
5479
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- Hello, I have a very simple problem but cannot seem to figure it out. I have a very simple php script that sends a test email to myself. When I debug it in PHP designer, it works with no problems, I get the test email. If
9
2355
by: santosh | last post by:
Hello all, I've put together a small program to count the number of characters and 'words' in a text file. The minimum length of a word, (in terms of no. of characters), as well as word delimiting characters can be specified on the command line. The default delimiting characters built into the program are space, newline, tab, carriage return, form feed, vertical tab, comma and null. If a 'u' or 'U' is specified as the last command line...
0
5573
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
2
1723
by: =?gb2312?B?wfXquw==?= | last post by:
Hi folks, I am trying to add a default argument to a template function in a template class, here is the code snippet: template<typename T> class Test { template<typename U> friend void doSomething(U level); };
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:
0
9563
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
9386
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
10145
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...
1
9938
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
9822
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
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
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
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2793
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.