Need VC6 workaround for function template

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hello!

I have created a function template that splits a string into a vector of tokens. To make it easy to use I overloaded it for strings so that it then looks like a regular function. For example, here's how it can be used:

{
std::string s("RTCM RTK,CMR,RTCM DGPS,RTCM V3,Leica");
std::vector<std::string> tokens = SF::Split(s, ",");
}
{
std::string s("1,2,3,4,5");
std::vector<int> tokens = SF::Split<int>(s, ",");
}

So, if the tokens are to be split into strings, not template parameter is needed. The problem is that the code compiles with VC7.1 but not VC6.0, where the compiler complains of ambiguous overload. Here is the code:

#include <string>
#include <vector>
#include <boost/lexical_cast.hpp>

namespace SF
{
template<class Target, class Source>
std::vector<Target>
Split(const Source& s, typename Source::const_pointer delims)
{
std::vector<Target> result;
typename Source::size_type first = 0;
typename Source::size_type last = s.find_first_of(delims);
while( last!=Source::npos )
{
result.push_back(boost::lexical_cast<Target>(s.substr(first, last-first)));
first = last + 1;
last = s.find_first_of(delims, first);
}
result.push_back(boost::lexical_cast<Target>(s.substr(first)));

return result;
}

std::vector<std::string> Split(const std::string& s, std::string::const_pointer delims)
{
return Split<std::string>(s, delims); // Error is here
}

std::vector<std::wstring> Split(const std::wstring& s, std::wstring::const_pointer delims)
{
return Split<std::wstring>(s, delims);
}
}

int main()
{
{
std::string s("RTCM RTK,CMR,RTCM DGPS,RTCM V3,Leica");
std::vector<std::string> tokens = SF::Split(s, ",");
}
{
std::wstring s(L"RTCM RTK,CMR,RTCM DGPS,RTCM V3,Leica");
std::vector<std::wstring> tokens = SF::Split(s, L",");
}
{
std::string s("1,2,3,4,5");
std::vector<int> tokens = SF::Split<int>(s, ",");
int d = tokens[0];
}
{
std::wstring s(L"1,2,3,4,5");
std::vector<int> tokens = SF::Split<int>(s, L",");
int d = tokens[0];
}
{
std::string s("1,2,3,4,5");
std::vector<double> tokens = SF::Split<double>(s, ",");
double d = tokens[0];
}
{
std::wstring s(L"1,2,3,4,5");
std::vector<double> tokens = SF::Split<double>(s, L",");
double d = tokens[0];
}

return 0;
}

VC6 says main.cpp(30) : error C2668: 'Split' : ambiguous call to overloaded function
This is in the std::vector<std::string> Split(const std::string& s, std::string::const_pointer delims)
function. Is there a workaround to make this code compile with VC6?
Thanks in advance!

--
Daniel
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.



Relevant Pages

  • Re: Please, please, please Help !!!
    ... I have a set of 100,000 ascii strings, up to 255 chars each. ... Each string has 1 or more words (tokens), ... if I have strings in: mary poppins, brad pitt, yygr ...
    (microsoft.public.sqlserver.xml)
  • Re: Stupid Newbie Needs Help
    ... Without the loop the program works fine with the ... with 0-terminated strings, that way you can take advantage of C's ... hold up to 10 tokens, where each token may be up to 80 characters ... should give a clue of how the variable/constant/function/macro is ...
    (comp.lang.c)
  • Use of symbols for "words" -- was Avoiding eval
    ... >using strings for your various categories rather than symbols. ... Some innocent looking tokens represent numbers so large lispworks 4.2 could ... What I think I need is a mechanism to cause lisp to interpret all tokens in context as ... typecase function on each of the arguments then somehow arrange for the ...
    (comp.lang.lisp)
  • Re: need an algo for string sorting
    ... I have to sort them. ... It really isn't a sorting problem, ... that, for example, "ax" is two tokens, but 'ai' is one token. ... you know the strings are unequal. ...
    (comp.programming)
  • (Heres how to do it) Re: Please help me with algorithms !!!
    ... FIRST PART (IDENTIFY YOUR ALGORITHM AND DATA STRUCTURE) ... You have 10,000 strings (you have to store them in something: ... Each string has 1 or more words (tokens), ... if I have strings in: mary poppins, brad pitt, yygr ...
    (comp.lang.c)