By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,507 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,507 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
* 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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.