Topic: std::string operator+


Author: Michiel Salters<Michiel.Salters@cmg.nl>
Date: Mon, 11 Jun 2001 08:43:10 GMT
Raw View
In article <5b15f8fd.0106070341.55f891b9@posting.google.com>, Dietmar Kuehl
says...
>
>Hi,
>"Matthew Darwin" <mattsjunkemail@yahoo.com> wrote:
>> The need to add a number to a string happens relatively frequently.  Always
>> defining a temp stringstream for this operation seems cumbersome.
>
>You can use something like
>
>  str + lexical_cast<std::string>(17);
>
>where 'lexical_cast<>()' is either taken from Boost or implemented something
>like this:
>
>  template <typename R, typename T>
>  R lexical_cast(T const& t) {
>    std::stringstream stream;
>    stream << t;
>    R rc;
>    if (!(stream >> rc))
>      throw std::bad_cast("lexical cast failed");
>    return rc;
>  }
As this is approx. the boost implementation I'll ask the question here:
why isn't this or the boost version specialized for std::string? Currently
I don't have a problem with it, because I don't use classes which output
embedded spaces in their representation. But if class X writes "a b",
casting X to a string should give "a b", not "a".

Regards,
Michiel Salters

--
Michiel Salters
Consultant Technical Software Engineering
CMG Trade, Transport & Industry
Michiel.Salters@cmg.nl

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: dietmar_kuehl@yahoo.com (Dietmar Kuehl)
Date: Thu, 7 Jun 2001 17:41:36 GMT
Raw View
Hi,
"Matthew Darwin" <mattsjunkemail@yahoo.com> wrote:
> The need to add a number to a string happens relatively frequently.  Always
> defining a temp stringstream for this operation seems cumbersome.

You can use something like

  str + lexical_cast<std::string>(17);

where 'lexical_cast<>()' is either taken from Boost or implemented something
like this:

  template <typename R, typename T>
  R lexical_cast(T const& t) {
    std::stringstream stream;
    stream << t;
    R rc;
    if (!(stream >> rc))
      throw std::bad_cast("lexical cast failed");
    return rc;
  }
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Matthew Darwin" <mattsjunkemail@yahoo.com>
Date: Mon, 4 Jun 2001 20:03:57 GMT
Raw View
My question is in regards to the std::string class.  Why doesn't std::string
overload the + operator to take integers or doubles?

The need to add a number to a string happens relatively frequently.  Always
defining a temp stringstream for this operation seems cumbersome.

I am guessing numeric formatting has something to do with the reason (?).

Thanks,
Matthew.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Michiel Salters<Michiel.Salters@cmg.nl>
Date: Wed, 6 Jun 2001 20:17:12 GMT
Raw View
In article <OGNS6.481$ui4.233329@news.uswest.net>, Matthew Darwin says...
>
>My question is in regards to the std::string class.  Why doesn't std::string
>overload the + operator to take integers or doubles?
>
>The need to add a number to a string happens relatively frequently.  Always
>defining a temp stringstream for this operation seems cumbersome.
>
>I am guessing numeric formatting has something to do with the reason (?).
>
>Thanks,
>Matthew.

That, and the confusion generated by

const char mystr[] = "ABC";
std::cout << ( mystr + 1 ) // == BC
std::string mystr2 = "ABC";
std::cout << ( mystr2 + 1)  // == ABC1 with your proposal.

C++ includes most of C, including the mess it made
from string (literals).

Regards,
Michiel Salters

--
Michiel Salters
Consultant Technical Software Engineering
CMG Trade, Transport & Industry
Michiel.Salters@cmg.nl

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]