samedi 28 mars 2015

Set default comparison type in Template


I want to set default class type as default comparison type in Template and then I want to compares two character strings using templates, I did write code but it’s giving error. Code and error is given below,



class CaseSenCmp{
public:
static int isEqual(char x, char y){ return x==y; }
};

template<typename c=CaseSenCmp>
int compare(char* str1, char* str2){
for(int i=0; i<strlen(str1) && i<strlen(str2); i++)
if(!c::isEqual(str1[i], str2[i]))
return str1[i]-str2[i];
return strlen(str1)-strlen(str2);
}

main(){
char *x = "hello", *y = "HELLO";
compare(x,y);
}


But when i have added this prototype of template, it works



template<typename c>


Compiler gives this error



error: default template arguments may not be used in function templates without -std=c++11 or -std=gnu++11|


Also when i try to do type casting in main function using this code, it also works



compare<CaseSenCmp>(x,y);


But i want to set Default Policy




Aucun commentaire:

Enregistrer un commentaire