473,396 Members | 1,918 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,396 software developers and data experts.

problem about "default template arguments may not be used in function templates "

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;
using std::string;
using std::char_traits;
using std::allocator;

template<class T> class Cmp {
public:
static bool eq(const T& c1, const T& c2) { c1 == c2; }
static bool lt(const T& c1, const T& c2) { c1 < c2; }
};

template< class Char, class T , class A , class C = Cmp<Char> >
inline int compare(const basic_string<Char, T, A>& str1, const
basic_string<Char, T, A>& str2) { // this compile error.
for(int i=0; i<str1.length() && i<str2.length(); i++)
if (!C::eq(str1[i], str2[i]))
return C::lt(str1[i], str2[i])?-1:1;
return str1.length() - str2.length();
}

int main(void) {
string s1 = "abcdefg";
string s2 = "ABCDEFG";

std::cout << compare(s1, s2) << std::endl;

std::cin.get();
}
but when compiling, the diagnostic "default template arguments may not be
used in function templates"
occur in line commented out. why?

thanks
sods
Dec 4 '05 #1
4 7215
Well, this is not the real problem with your code.
with cl.exe (VC6) it says:
warning C4519: default template arguments are only allowed on a class
template; ignored
Which means, you cannot define default arguments (i.e. "class C =
Cmp<char>") in a function template (compare is a function template). It
is only allowed in class templates.
VC6 decides to ignores this... which OK until you are trying to use the
function template.
That's when you have a problem.
When you try to use _complare_ on s1,s2 the compiler can deduce the
type of Char, T and A, but not C (cause the default arguments are not
allowed).
And here is the error I get:
error C2783: 'int __cdecl compare(const class
std::basic_string<BasicChar,T,A> &,const class
std::basic_string<BasicChar,T,A> &)' : could not deduce template
argument for 'C'

You can do instead, something like this:
template< class BasicString, class C >
inline int compare2(const BasicString& str1, const BasicString& str2)
{ // this compile error.
for(int i=0; i<str1.length() && i<str2.length(); i++)
if (!C::eq(str1[i], str2[i]))
return C::lt(str1[i], str2[i])?-1:1;
return str1.length() - str2.length();
}

and the call:
std::cout << compare2 <string, Cmp<char> >(s1, s2) << std::endl;

can't think of something else right now...

Dec 4 '05 #2
Yeah, I think it's the cause.

but my code is from the TC++PL(3rdEdition) ,
13.4.1 Default Template Parameter, with less alter.

it says:
"Alternatively, we can supply the normal convention as a default template
argument:
template<class T, class C =Cmp<T> >
int compare(const String<T>& str1, const String<T>& str2)
{
for(int i=0; i<str1.length() && i< str2.length() ; i++)
if (!C: :eq(str1[i] ,str2[i])) return C: :lt(str1[i] ,str2[i]) ? 1
: 1;
return str1.length()str2.
length() ;
}"
and this code can't pass ,too.

it's surprising , isn't it.

sods
<gu*****@gmail.com> дÈëÓʼþ
news:11**********************@g49g2000cwa.googlegr oups.com...
Well, this is not the real problem with your code.
with cl.exe (VC6) it says:
warning C4519: default template arguments are only allowed on a class
template; ignored
Which means, you cannot define default arguments (i.e. "class C =
Cmp<char>") in a function template (compare is a function template). It
is only allowed in class templates.
VC6 decides to ignores this... which OK until you are trying to use the
function template.
That's when you have a problem.
When you try to use _complare_ on s1,s2 the compiler can deduce the
type of Char, T and A, but not C (cause the default arguments are not
allowed).
And here is the error I get:
error C2783: 'int __cdecl compare(const class
std::basic_string<BasicChar,T,A> &,const class
std::basic_string<BasicChar,T,A> &)' : could not deduce template
argument for 'C'

You can do instead, something like this:
template< class BasicString, class C >
inline int compare2(const BasicString& str1, const BasicString& str2)
{ // this compile error.
for(int i=0; i<str1.length() && i<str2.length(); i++)
if (!C::eq(str1[i], str2[i]))
return C::lt(str1[i], str2[i])?-1:1;
return str1.length() - str2.length();
}

and the call:
std::cout << compare2 <string, Cmp<char> >(s1, s2) << std::endl;

can't think of something else right now...

Dec 6 '05 #3
* sods:
[top-posting]
[over-quoting]
Please don't top-post in this group, or in any non-Microsoft Usenet
group.

Please don't quote extranous stuff, either.
* sods: * gu*****@gmail.com:

with cl.exe (VC6) it says:
warning C4519: default template arguments are only allowed on a class
template; ignored


Yeah, I think it's the cause.

but my code is from the TC++PL(3rdEdition) ,
13.4.1 Default Template Parameter, with less alter.

it says:
"Alternatively, we can supply the normal convention as a default template
argument:
template<class T, class C =Cmp<T> >
int compare(const String<T>& str1, const String<T>& str2)


<url: http://www.research.att.com/~bs/3rd_issues.html>
"There are examples in my book that are in error according to the
standard but that I haven't changed because there is a defect report on
the issue."
....
"*pg 340: Due to an unfortunate oversight, the standard simply bans
default arguments for template parameters for a function template. Voted
to be corrected in the next standard."

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 6 '05 #4

"Alf P. Steinbach" <al***@start.no> дÈëÓʼþ
news:43*****************@news.individual.net...
* sods:
[top-posting]
[over-quoting]


Please don't top-post in this group, or in any non-Microsoft Usenet
group.

Please don't quote extranous stuff, either.
* sods:
* gu*****@gmail.com:

with cl.exe (VC6) it says:
warning C4519: default template arguments are only allowed on a class
template; ignored


Yeah, I think it's the cause.

but my code is from the TC++PL(3rdEdition) ,
13.4.1 Default Template Parameter, with less alter.

it says:
"Alternatively, we can supply the normal convention as a default template argument:
template<class T, class C =Cmp<T> >
int compare(const String<T>& str1, const String<T>& str2)


<url: http://www.research.att.com/~bs/3rd_issues.html>
"There are examples in my book that are in error according to the
standard but that I haven't changed because there is a defect report on
the issue."
...
"*pg 340: Due to an unfortunate oversight, the standard simply bans
default arguments for template parameters for a function template. Voted
to be corrected in the next standard."

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

I'll take care. sorry for that.
thanks for reminding.
Also, thanks your reply. I got the key of problem.

sods.

Dec 6 '05 #5

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

Similar topics

6
by: DJ Majestik | last post by:
OK, I am devising a php page that will handle a form submission, and wanted to know if anyone has already setup such an idea, or if you had links to point to good tutorials on this. Basically I...
0
by: Gianni Mariani | last post by:
I remember seeing a neat template specialization trick posted here a few months ago that allowed the detection of a type containing a member. After a day searching through google archives I come up...
13
by: DaKoadMunky | last post by:
I recently came across some code in a template that default constructed an object of type T to pass to another function... SomeFunction(T()); The code that instantiates that template specifies...
1
by: Perttu Pulkkinen | last post by:
I would like to have a javasript/jscript function template/"framwork" instead of checking browsers by name. The shortness of script in my opinion is not the goal, but clearness and ease iof...
7
by: Eric Laberge | last post by:
Aloha! This question is meant to be about C99 and unnamed compound objects. As I read, if such a construct as int *p = (int){0}; is used within a function, then it has "automatic storage...
18
by: Clark Nu | last post by:
It seems that when I define a fuction,I can set a default value to some of the peremeters.When I call the fuction without some of them,the fuction will use the default value automaticlly then...
4
by: Gary li | last post by:
Hi, all I find "template template" class cann't been compiled in VC6 but can ok in Redhat9. I write a test program like as: template< template<class> class T> class A { }; int main() {...
7
by: runsun pan | last post by:
I wanna check if an object is the *arguments* object of a function, is that possible? When we do: typeof(arguments) it always returns "object", doesn't seem to be helpful.
1
by: Rob | last post by:
I am doing a simple setup deployment.... everything is fine but the program installs to the Program Files\Default Company Name.... I hear that is derived from the "Manufacturer".... Where do...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...

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.