Форум » C/C++ для начинающих (C/C++ for beginners) » Overriding return type in function template » Ответить

Overriding return type in function template

ramees: how can i Overriding return type in function template . i create a string template function i expecting to return int data

Ответов - 3

Сыроежка: It is not clear what exactly you try to do. Could you show some code of the function declaration (or definition) that it would be more clear?

ramees: [pre2] #include <iostream> #include <string> using namespace std; template <typename Type> Type maxof (Type a , Type b ){ if(a == b){ int r = 8; } int r = 0 ; return r; } int main () { cout<<maxof ("hh" , "kk")<<endl; return 0; } [/pre2]

Сыроежка: You can define a non-template function with the same name that has parameters of the required type. For example [pre2] #include <iostream> #include <string> template <typename Type> const Type & maxof( const Type &a , const Type &b ) { return a < b ? b : a; } std::string::size_type maxof( const std::string &a, const std::string &b ) { std::string::size_type r = 0; if ( a == b ) r - 8; return r; } std::string::size_type maxof( const char *a, const char *b ) { return maxof( std::string( a ), std::string( b ) ); } int main() { std::cout << maxof( "hh" , "kk" ) << std::endl; std::cout << maxof( 20 , 40 ) << std::endl; } [/pre2] The program output is [pre2] 0 40 [/pre2]




полная версия страницы