This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Problem with function template specialization
- From: Patrick Percot <ppercot at free dot fr>
- To: gcc-help at gcc dot gnu dot org
- Cc: conveycj at npt dot nuwc dot navy dot mil
- Date: Thu, 30 Dec 2004 23:15:18 +0100 (CET)
- Subject: Re: Problem with function template specialization
- References: <41D43117.7080203@npt.nuwc.navy.mil>
On Thu, 30 Dec 2004 11:47:19 -0500, Christian Convey <conveycj@npt.nuwc.navy.mil> wrote
> Hello,
Hi,
>
> I'm trying to write a small library of function templates, all named
> "toString", that will produce a string rendition of various data types.
>
> My problem is that when I try to create a specialized version of the
> toString() function for a particular data type, I get an error:
>
Don't you think that operator overloading is a better and more elegant
solution ?
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
class Foo {
private:
int x;
int y;
public:
Foo(int xv, int yv)
: x(xv)
, y(yv) {};
friend ostream& operator<< (ostream& os, const Foo& f);
};
ostream& operator<< (ostream& os, const Foo& f) {
os << "(" << &f << ") = Foo(" << f.x << ", " << f.y << ")";
}
template <typename T>
string toString(const T& val) {
ostringstream os;
os << val;
return os.str();
}
int main() {
Foo f(1,2);
cout << toString(f) << endl;
}
[..Zappé 59 lignes et 1233 caractères..]
À+
PP
--
NE MANGEZ PAS DE FOIE GRAS : http://www.stopgavage.com
Groupe Morbihannais d'Utilisateurs de Logiciels Libres http://www.tuxbihan.org
GPG fingerprint = 1A4F E154 3D2C A20E E4CA A543 7951 C5C2 E44A A0B5
Patrick Percot.