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 4 7045
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...
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...
* 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?
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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());
...
|
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...
|
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...
|
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...
|
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<...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...
| |