C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Comment écrire to_string? (long)

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
SerGioGio
Guest





PostPosted: Thu May 19, 2005 3:23 am    Post subject: Comment écrire to_string? (long) Reply with quote



Bonjour,

Je cherche a écrire une fonction to_string semblable à boost::lexical_cast
mais
1)dans un seul sens (T -> std::string) et 2)permettant d'utiliser des
manipulateurs (std::hex / std::showbase / etc.). J'ai fait plusieurs essais,
et
j'aimerais avoir votre opinion générale sur chacun d'eux (d'un point de vue
pratique, à l'utilisation) et si il y a de meilleures solutions?

Merci d'avance pour vos commentaires et suggestions.

SerGioGioGio


**** solution1
Quote:
#define to_string(expr)
std::string(
static_cast<std::ostringstream&>(std::ostringstream().flush() << expr
).str().c_str())

pour l'utiliser:
Quote:
std::cout << to_string(std::hex << std::showbase << 10);

problèmes:
- c'est une macro
- ça plantera dès que quelqu'un dira (à cause de la virgule):
Quote:
std::cout << to_string( toto<1, 2>(10) );


**** solution2
Quote:
template <class T
std::string to_string(const T& a_t, std::ostream& a_stream = fmt())
{
a_stream << a_t;
return static_cast }

pour l'utiliser:
Quote:
std::cout << to_string(10, fmt() << std::hex << std::showbase);

problèmes:
- c'est lourd à écrire

**** solution3
Quote:
struct to_string : public std::string
{
std::stringstream stream;
template {
(*this)(a_t);
}
template<class T> to_string(T& a_t)
{
(*this)(a_t);
}
template<class T> to_string& operator() (const T& a_t)
{
stream << a_t;
append(
std::istreambuf_iterator std::istreambuf_iterator<char>());
return *this;
}
template<class T> to_string& operator() (T& a_t)
{
stream << a_t;
append(
std::istreambuf_iterator std::istreambuf_iterator<char>());
return *this;
}
};

pour l'utiliser:
Quote:
std::cout << to_string(std::hex)(std::showbase)(10);

problèmes:
- la ligne suivante est prise pour une définition de std::hex!!
Quote:
to_string(std::hex);

remarque:
- A t'on vraiment besoin des versions non-const dans ce cas là? VC7 ne
compile pas to_string(std::hex) sans les version non-const, alors que Comeau
online oui.




Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Thu May 19, 2005 7:58 am    Post subject: Re: Comment écrire to_string? (long) Reply with quote



SerGioGio wrote:

Quote:
Je cherche a écrire une fonction to_string semblable à
boost::lexical_cast mais
1)dans un seul sens (T -> std::string) et 2)permettant d'utiliser des
manipulateurs (std::hex / std::showbase / etc.).

Il existe une classe qui fait ça déjà dans la norme. Elle
s'appelle std::ostringstream. Elle est aussi beaucoup plus
souple à l'utilisation que tu ne pourrais faire avec une seule
fonction.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


Back to top
Pierre THIERRY
Guest





PostPosted: Thu May 19, 2005 12:03 pm    Post subject: Re: Comment écrire to_string? (long) Reply with quote



Le Thu, 19 May 2005 05:23:52 +0200, SerGioGio a écrit :
Quote:
Je cherche a écrire une fonction to_string [...] permettant d'utiliser
des manipulateurs

Si l'utilisation d'un stringstream ne te convient pas, tu pourrais
écrire un foncteur ; dans ce cas, c'est la solution la plus évidente. Tu
lui surcharges l'opérateur de décalage pour le type fmtflags et
l'opérateur () pour ton type T.

Brièvement,
Nowhere man
--
[email]nowhere.man (AT) levallois (DOT) eu.org[/email]
OpenPGP 0xD9D50D8A


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.