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.

1
  • 3
    I don't think we will ever get rid of the "Hay Dear!" :D Commented Feb 24, 2010 at 18:21

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

§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) { /* ... */ }
};

2 Comments

thought so, wasn't sure though.
then where we specify default template argument
2

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.

2 Comments

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?
i have got the point, yes you are right we can not give default template arguments in case of function templates
0

What i use is next trick;

lets say you want to have function like this

template <typename E, typename ARR_E = MyArray_t<E> > void doStuff(ARR_E array)
{
    E one(1);
    array.add( one );
}

you will not be allowed, but i do next way:

template <typename E, typename ARR_E = MyArray_t<E> >
class worker {
public:
    /*static - as you wish */ ARR_E* parr_;
    void doStuff(); /* do not make this one static also, MSVC complains */
};

template <typename E, typename ARR_E>
void worker::doStuff<E, ARR_E>::getChunks()
{
    E one(1);
    parr_->add( one );
}

so this way you may use it like this.

MyArray_t my_array;
worker<int> w;
w.parr_ = &arr;
w.doStuff();

as we can see no need to explicitly set second parameter. maybe it will be useful for someone.

1 Comment

Be careful when posting copy and paste boilerplate/verbatim answers to multiple questions, these tend to be flagged as "spammy" by the community.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.