//: C05:TemplateFunctionAddress.cpp {-mwcc} // Taking the address of a function generated // from a template. template<typename T> void f(T*) {} void h(void (*pf)(int*)) {} template<typename T> void g(void (*pf)(T*)) {} int main() { h(&f<int>); // Full type specification h(&f); // Type deduction g<int>(&f<int>); // Full type specification g(&f<int>); // Type deduction g<int>(&f); // Partial (but sufficient) specification } ///:~
Listado 6.18. C05/TemplateFunctionAddress.cpp
// The variable s is a std::string transform(s.begin(), s.end(), s.begin(), tolower);
//: C05:FailedTransform.cpp {-xo} #include <algorithm> #include <cctype> #include <iostream> #include <string> using namespace std; int main() { string s("LOWER"); transform(s.begin(), s.end(), s.begin(), tolower); cout << s << endl; } ///:~
Listado 6.19. C05/FailedTransform.cpp
template<class charT> charT toupper(charT c, const locale& loc); template<class charT> charT tolower(charT c, const locale& loc);
transform(s.begin(),s.end(),s.begin() static_cast<int(*)(int)>(tolower));
//: C05:StrTolower.cpp {O} {-mwcc} #include <algorithm> #include <cctype> #include <string> using namespace std; string strTolower(string s) { transform(s.begin(), s.end(), s.begin(), tolower); return s; } ///:~
Listado 6.20. C05/StrTolower.cpp
//: C05:Tolower.cpp {-mwcc} //{L} StrTolower #include <algorithm> #include <cctype> #include <iostream> #include <string> using namespace std; string strTolower(string); int main() { string s("LOWER"); cout << strTolower(s) << endl; } ///:~
Listado 6.21. C05/Tolower.cpp
//: C05:ToLower2.cpp {-mwcc} #include <algorithm> #include <cctype> #include <iostream> #include <string> using namespace std; template<class charT> charT strTolower(charT c) { return tolower(c); // One-arg version called } int main() { string s("LOWER"); transform(s.begin(),s.end(),s.begin(),&strTolower<char>); cout << s << endl; } ///:~
Listado 6.22. C05/ToLower2.cpp