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
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