Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- //degenerate case
- template<typename T>
- T& longest(T& a) {return a;}
- // regular case
- template<typename T>
- T& longest( T& a, T& b)
- {
- std::string A(a);
- std::string B(b);
- return (A.length() >= B.length())? a: b;
- }
- // works for std:string, not for "string"
- template<typename T, typename... Args>
- T& longest( Args... args )
- {
- return longest( args...) ;
- }
- int main ( int argc, char* argv[])
- {
- std::string a , b, c, d;
- a = "a";
- b = "ab";
- c = "abc";
- d = "abcd";
- std::string x = longest( a, b, c, d); // works fine
- /* error
- longest.cpp:66:19: note: in instantiation of function template specialization
- 'longest<char const[2], const char *, const char *, const char *>' requested here
- */
- std::string y = longest( "1" , "22", "333", "4444"); // why are different types? Remedy?
- std::cout << x << std::endl;
- std::cout << y << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement