3

I want to provide a templated function that converts most basic types to string. The best I have come up with so far is the following:

template<typename T> inline std::string anyToString(const T& var) {
  std::ostringstream o;
  o << var;
  return o.str();
}

The function can e.g. be used for the following:

 class TimeError:public std::runtime_error{
     public:
       explicit TimeError(int time):std::runtime_error(anyToString(time)),
       mTime(time){};
     protected:
       int mTime;
     };

The problem with anyToString and similar functions is the generation of ambiguity warnings when compiling with gcc version 4.4.3 -Wall -Wexta -Werror "ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second"

To my knowledge the reason for the warning lies in the implicit conversion possibilities when calling <<.

Those ambiguities are mainly generated by other templates as the following:

  template<typename T>
    T& operator<<(T& out, const SymRad& angle){
    return out << angle.deg();
  }

But those have other advantages like working for several stream types. So I would like to keep them. If I turn the second template into a plain method for e.g. ostream the ambiguity is cleaned, but I'm looking for sth. that allows keeping both templates. Is there a generic function that does provide the same simplicity without generating warnings using the described options ? If not, what is the best way to locally disable the issued warning ?

Martin
  • 4,738
  • 4
  • 28
  • 57

2 Answers2

2

It seems you'd get such a message from a scenario like this:

#include <sstream>
#include <string>
#include <iostream>

struct Y {};
struct X
{
    operator Y() const {return Y(); }
};

std::ostream& operator<< (std::ostream& os, X) { return os << "X"; }
std::ostream& operator<< (std::ostringstream& os, Y) { return os << "Y"; }

template<typename T> inline std::string anyToString(const T& var) {
  std::ostringstream o;
  o << var;
  return o.str();
}

int main()
{
    std::cout << anyToString(X()) << '\n';
}

I'd recommend using the -pedantic flag instead. GCC compiles it at all thanks to a compiler extension, with other compilers this will be a straight error.


As to your addition:

  template<typename T>
    T& operator<<(T& out, const SymRad& angle){
    return out << angle.deg();
  }

But those have other advantages like working for several stream types.

This actually doesn't work for several stream types. E.g if T is stringstream, then out << angle.deg(); will likely return a reference to an ostream which cannot be implicitly downcasted back to a stringstream reference.

UncleBens
  • 40,819
  • 6
  • 57
  • 90
  • Good example, I added the real source of evil to the description, but your example addresses just the same. -pedantic looks good on my code but I use several qt4 libraries and those are a nightmare in warning/error generation – Martin Nov 16 '10 at 16:09
  • @UncleBens: Good call on the template issue for `operator<<`, it's only compatible therefore with operators defined in a similar fashion. – Matthieu M. Nov 16 '10 at 16:22
  • @UncleBens: I thought the templated << operator would work e.g. for stringstreams and filestreams (std::ofstream and std::ostringstream). Correct me If I'm mistaken – Martin Nov 16 '10 at 16:38
  • @Martin: Try it out yourself. Existing `operator<<` for those types still returns `ostream&`. The correct way to overload `operator<<` for standard library stream is to do so for `ostream&` – UncleBens Nov 16 '10 at 17:35
  • @UncleBens (a bit of nitpicking): Better overload for `std::basic_ostream` than `std::ostream`: `template std::basic_ostream& operator<<(std::basic_ostream& stream, const Type& object) { }` – Philipp Nov 16 '10 at 17:55
1

Your compiler supports a #pragma for this purpose, as far as I know - I know that VC++ does. However, you could just use a boost::lexical_cast, also.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • `boost::lexical_cast` fails with the same message for a similar set of overloads as demonstrated in my answer (except it uses `stringstream`). – UncleBens Nov 16 '10 at 15:55
  • boost::lexical cast does the trick, though I have to install and include boost for this and other people who will use the template too. – Martin Nov 16 '10 at 16:19