Re: Need VC6 workaround for function template
Daniel Lidstr?m wrote:
namespace SF
{
Just one suggestion here, though not related to the problem at hand: you can
use a real name for the namespace (namespace sweet_functions {...}) and
then use a namespace alias (namespace sf = sweet_functions;) to keep code
short.
template<class Target, class Source>
std::vector<Target>
Split(const Source& s, typename Source::const_pointer delims)
{...}
std::vector<std::string> Split(const std::string& s,
std::string::const_pointer delims)
{...}
std::vector<std::wstring> Split(const std::wstring& s,
std::wstring::const_pointer delims)
{...}
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?
I think that the trick is to first define the normal functions and then the
templates. This might require creating another template function (e.g.
DoSplit) which the two concrete ones and the template delegate their calls
to.
There's another problem still: VC6's compiler has a bug that it only
includes the parameters that are passed to a function into the mangled
name. For normal functions, which you can't overload on their return type
that is enough, but for template functions it means that you must
artificially introduce the return-type into the passed parameters. In your
case I don't understand why the Target type needs to be different than the
Source type, but it would look like this:
template<typename Target, typename Source>
vector<Target>
Split( Source const& s, typename Source::const_pointer delims
#if defined(_MSC_VER) && _MSC_VER<1300
// compilers before that of VC7 fail to mangle all template parameters
// into the symbol, introduce them here artificially.
, Target const* =0
#endif
)
{...}
Other than that, projects like STLport or Boost are full of cludges to make
them work sufficiently with old, sub-standard compilers like that of VC6.
You might get some wisdom there.
Uli