R
Ricky65
I was thinking about string formatting after reading this article by
Herb Sutter here:
http://www.gotw.ca/publications/mill19.htm
I thought to myself "Why haven't they overloaded the << operator for
the basic_string class?" like with the iostream. I don't think I'm the
first to have thought of this. I assume performance problems are the
reason this is not done.
This would eliminate the need to make a temporary variable to hold the
formatted data like a stringstream does and the problems with the
sprintf family. I know a lot of people shun stringstreams because they
are unacceptably slow in some cases.
For example, we could do something like this:
int cat_len = 120;
int mouse_len = 29;
std::string myformattedstring << "The cat is " << a << "cm tall and
the mouse is " << b << "cm tall.";
As you can see, this would format directly to the string. This would
be both type safe and length safe and the programmer wouldn't have to
allocate a temporary variable for a stream. However, I'm not sure how
efficient it would be. I'm intrigued to know if this can be done and
and if not, the reasons why.
Thanks
Ricky Marcangelo
Herb Sutter here:
http://www.gotw.ca/publications/mill19.htm
I thought to myself "Why haven't they overloaded the << operator for
the basic_string class?" like with the iostream. I don't think I'm the
first to have thought of this. I assume performance problems are the
reason this is not done.
This would eliminate the need to make a temporary variable to hold the
formatted data like a stringstream does and the problems with the
sprintf family. I know a lot of people shun stringstreams because they
are unacceptably slow in some cases.
For example, we could do something like this:
int cat_len = 120;
int mouse_len = 29;
std::string myformattedstring << "The cat is " << a << "cm tall and
the mouse is " << b << "cm tall.";
As you can see, this would format directly to the string. This would
be both type safe and length safe and the programmer wouldn't have to
allocate a temporary variable for a stream. However, I'm not sure how
efficient it would be. I'm intrigued to know if this can be done and
and if not, the reasons why.
Thanks
Ricky Marcangelo