Hi, I am trying to write function overloaded function template, but I get some kind of an instantiation problem and here is my code:
/*
* File: main.cpp
* Author: cppguy
*
* Created on 20 January 2011, 19:10
*/
#include <cstdlib>
#include <iostream>
#include <cctype>
#include <cwctype>
#include <typeinfo>
using namespace std;
/* */
template<typename t, typename funct_>
void trimr(std::basic_string<t>& s, funct_ f) {
if (s.empty())
return;
typename std::basic_string<t>::iterator it;
for (it = s.end(); it != s.begin() && *--it == f(*it););
if (*it != f(*it))
++it;
s.erase(it, s.end());
}
template<typename t>
void trimr(std::basic_string<t>& s) {
if (typeid (std::basic_string<char>()) == typeid (s))
trimr(s, isspace); // no matching function call
else
trimr(s, iswspace); //no matching function call
}
int main(int argc, char** argv) {
string s("zoo");
wstring ws(L"fee");
trimr(s);
trimr(ws);
cout << s << endl;
wcout << ws << endl;
return 0;
}
And compiler gives this nice msg:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/cppguy/NetBeansProjects/whitespace_triming'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/whitespace_triming
make[2]: Entering directory `/home/cppguy/NetBeansProjects/whitespace_triming'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
main.cpp: In function ‘void trimr(std::basic_string<_CharT, std::char_traits<_CharT>, std::allocator<_CharT> >&) [with t = char]’:
main.cpp:41: instantiated from here
main.cpp:32: error: no matching function for call to ‘trimr(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, <unresolved overloaded function type>)’
main.cpp: In function ‘void trimr(std::basic_string<_CharT, std::char_traits<_CharT>, std::allocator<_CharT> >&) [with t = wchar_t]’:
main.cpp:42: instantiated from here
main.cpp:32: error: no matching function for call to ‘trimr(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&, <unresolved overloaded function type>)’
make[2]: *** [build/Debug/GNU-Linux-x86/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
make[2]: Leaving directory `/home/cppguy/NetBeansProjects/whitespace_triming'
make[1]: Leaving directory `/home/cppguy/NetBeansProjects/whitespace_triming'
BUILD FAILED (exit value 2, total time: 362ms)