tags:

views:

151

answers:

3

Suppose i have a function template StrCompare

template<typename T=NonCaseSenCompare>//NonCaseSenCompare is a user defined class look at the detailed code below.
int StrCompare(char* str1, char* str2)
{
...
}

now in the main function i write a line

char* str1="Zia";
char* str2="zia";
int result=StrCompare(str1,str2);

it should work because we have provided a default template argument, but it does'nt
compiler gives the following error
no matching function for call to `StrCompare(char*&, char*&)' Now the detailed code is given by

#include<iostream.h>
class CaseSenCompare
{
public: 
static int isEqual(char x, char y)
{
return x==y;
}
};
class NonCaseSenCompare
{
public:
static int isEqual(char x,char y)
{
char char1=toupper(x);
char char2=toupper(y);
return char1==char2;
}
};
template<typename T=NonCaseSenCompare>
int StrCompare(char* str1, char* str2)
{
for(int i=0;i < strlen(str1)&& strlen(str2);i++)
{
if(!T::isEqual(str1[i],str2[i]))
return str1[i]-str2[i];
}
return strlen(str1)-strlen(str2);
}

main()
{
char* ptr1="Zia ur Rahman";
char* ptr2="zia ur Rahman";
int result=StrCompare(ptr1,ptr2);//compiler gives error on this line
cout<<result<<endl;
system("pause");
}

If I write

int result=StrCompare<>(ptr1,ptr2);

compiler gives the same error message.

+3  A: 

§14.1/9:

A default template-argument shall not be specified in a function template declaration or a function template definition, nor in the template-parameter-list of the definition of a member of a class template.

A simple work-around would be to move it into a class:

template<typename T=NonCaseSenCompare>
struct StrCompare {
    static int compare(char* str1, char* str2) { /* ... */ }
};
Georg Fritzsche
thought so, wasn't sure though.
Hassan Syed
then where we specify default template argument
Zia ur Rahman
+2  A: 

Firstly, function templates do not support default template arguments, only class templates do.

Secondly, even when all class template parameters have default arguments, you still have to specify an empty <> to refer to that class template.

AndreyT
Is it mean that we can not give default template arguments in case of function templates , we can only give default template arguments in case of class member functions?
Zia ur Rahman
Re think about it, Our teacher said we can do it
Zia ur Rahman
i have got the point, yes you are right we can not give default template arguments in case of function templates
Zia ur Rahman
@Zia, Only C++0x allows this
Johannes Schaub - litb
+4  A: 

As gf and AndreyT already wrote, you can't have default template arguments with function templates. However, if you turn your comparators into function objects, you can still use default function arguments:

template<typename Comp>
int StrCompare(char* str1, char* str2, Comp = NonCaseSenCompare())
{
  ...
}

You can now call StrCompare() like this

StrCompare("abc","aBc",CaseSenCompare());

or like this:

StrCompare("abc","aBc"); // uses NonCaseSenCompare

A comparator would then have to look like this:

struct CaseSenCompare {
  bool operator()(char x, char y) const {return x==y;}
};

Adjust StrCompare() accordingly.

sbi
yes, i have got the point we can not give default template arguments incase of function templates. Thanks
Zia ur Rahman