473,398 Members | 2,125 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,398 software developers and data experts.

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 misunderstanding 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 2105
"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 misunderstanding 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
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...
4
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...
3
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...
2
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
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 -------------------------------------------------------------------------------- ...
9
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...
0
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...
2
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...
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:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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,...
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.