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

S

sods

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, str2))
return C::lt(str1, str2)?-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
 
G

guyarad

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, str2))
return C::lt(str1, str2)?-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...
 
S

sods

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 ,str2)) return C: :lt(str1 ,str2) ? 1
: 1;
return str1.length()str2.
length() ;
}"
and this code can't pass ,too.

it's surprising , isn't it.

sods
 
A

Alf P. Steinbach

* 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:
* (e-mail address removed):

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."
 
S

sods

Alf P. Steinbach said:
* 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:
* (e-mail address removed):

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top