By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,956 Members | 1,457 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,956 IT Pros & Developers. It's quick & easy.

std::string = char* + std::string

P: n/a
#include <string>

int main ()
{
std::string MyString = "Testing";
MyString = " " + MyString;
}

This works in Microsoft Visual C++ .net 2003

The end result being MyString contans the text " Testing"

I'm wondering if this is UB. Apparently MSVC is converting
the " " to a std::string, then doing the +. Otherwise it would
try to add a std::string to a char* which would fail.

I know I can do MyString = std::string(" ") + MyString; and
all would still work. But I'm wondering if I have to (to stay
within the standards).
Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
* Jim Langston:
#include <string>

int main ()
{
std::string MyString = "Testing";
MyString = " " + MyString;
}

This works in Microsoft Visual C++ .net 2003

The end result being MyString contans the text " Testing"

I'm wondering if this is UB.


No.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a
>This works in Microsoft Visual C++ .net 2003
The end result being MyString contans the text " Testing" I'm wondering if this is UB. Apparently MSVC is converting
the " " to a std::string, then doing the +. Otherwise it would
try to add a std::string to a char* which would fail. I know I can do MyString = std::string(" ") + MyString; and
all would still work. But I'm wondering if I have to (to stay
within the standards).


So what do you want?

Jul 23 '05 #3

P: n/a
"Son of Sam" <so*@ofpelite.de> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
This works in Microsoft Visual C++ .net 2003

The end result being MyString contans the text " Testing"

I'm wondering if this is UB. Apparently MSVC is converting
the " " to a std::string, then doing the +. Otherwise it would
try to add a std::string to a char* which would fail.

I know I can do MyString = std::string(" ") + MyString; and
all would still work. But I'm wondering if I have to (to stay
within the standards).


So what do you want?


I want to know if this behavior follows the C++ standards,
or is just something that MSVC++ .net 2003 does.

Will this work on other compilers, so will my code be
portable, or is MSVC doing it's own thing here.
Jul 23 '05 #4

P: n/a
* Jim Langston:
"Son of Sam" <so*@ofpelite.de> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
This works in Microsoft Visual C++ .net 2003

The end result being MyString contans the text " Testing"

I'm wondering if this is UB. Apparently MSVC is converting
the " " to a std::string, then doing the +. Otherwise it would
try to add a std::string to a char* which would fail.

I know I can do MyString = std::string(" ") + MyString; and
all would still work. But I'm wondering if I have to (to stay
within the standards).


So what do you want?


I want to know if this behavior follows the C++ standards,
or is just something that MSVC++ .net 2003 does.

Will this work on other compilers, so will my code be
portable, or is MSVC doing it's own thing here.


What was the problem with answer you've already got?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #5

P: n/a
Jim Langston wrote:
I want to know if this behavior follows the C++ standards,
or is just something that MSVC++ .net 2003 does.

Will this work on other compilers, so will my code be
portable, or is MSVC doing it's own thing here.


I believe your question is completely valid. I also believe Alf gave you
his version of a complete answer with elaboration. If I'm not having an
attack of dyslexia, this says what Alf said:

ISO/IEC 14882:2003(E) 21.3.7.1
template<class charT, class traits, class Allocator>
basic_string<charT,traits,Allocator>
operator+(const charT* lhs,
const basic_string<charT,traits,Allocator>& rhs);
Returns: basic_string<charT,traits,Allocator>(lhs) + rhs.

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #6

P: n/a
Jim Langston wrote:
#include <string>

int main ()
{
std::string MyString = "Testing";
MyString = " " + MyString;
}

This works in Microsoft Visual C++ .net 2003

The end result being MyString contans the text " Testing"

I'm wondering if this is UB. Apparently MSVC is converting
the " " to a std::string, then doing the +. Otherwise it would
try to add a std::string to a char* which would fail.

I know I can do MyString = std::string(" ") + MyString; and
all would still work. But I'm wondering if I have to (to stay
within the standards).


The standard defines (section 21.2):

template<class charT, class traits, class Allocator>
basic_string<charT,traits,Allocator> operator+(const charT* lhs, const
basic_string<charT,traits,Allocator>& rhs);

Getting rid of the template mess, you can think of that as:

std::string operator+(const char* lhs, const std::string& rhs);

Your line:
MyString = " " + MyString;

works fine because " " can be implicitly converted to a const char*.

-Alan

Jul 23 '05 #7

P: n/a
Alf P. Steinbach wrote:
I want to know if this behavior follows the C++ standards,
or is just something that MSVC++ .net 2003 does.

Will this work on other compilers, so will my code be
portable, or is MSVC doing it's own thing here.


What was the problem with answer you've already got?


Maybe that it doesn't answer the above.

Jul 23 '05 #8

P: n/a
* Rolf Magnus:
Alf P. Steinbach wrote:
I want to know if this behavior follows the C++ standards,
or is just something that MSVC++ .net 2003 does.

Will this work on other compilers, so will my code be
portable, or is MSVC doing it's own thing here.


What was the problem with answer you've already got?


Maybe that it doesn't answer the above.


It did, as clearly as possible. But apart from the OP's problem with
that I'd be interested to know why you think that "maybe" it didn't.
Do you have some reasoning to offer, or are you again just blowing wind?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #9

P: n/a

"Alan Johnson" <al****@stanford.dot.nospam_edu> wrote in message
news:da**********@news.Stanford.EDU...
Jim Langston wrote:
#include <string>

int main ()
{
std::string MyString = "Testing";
MyString = " " + MyString;
}

This works in Microsoft Visual C++ .net 2003

The end result being MyString contans the text " Testing"

I'm wondering if this is UB. Apparently MSVC is converting
the " " to a std::string, then doing the +. Otherwise it would
try to add a std::string to a char* which would fail.

I know I can do MyString = std::string(" ") + MyString; and
all would still work. But I'm wondering if I have to (to stay
within the standards).


The standard defines (section 21.2):

template<class charT, class traits, class Allocator>
basic_string<charT,traits,Allocator> operator+(const charT* lhs, const
basic_string<charT,traits,Allocator>& rhs);

Getting rid of the template mess, you can think of that as:

std::string operator+(const char* lhs, const std::string& rhs);

Your line:
MyString = " " + MyString;

works fine because " " can be implicitly converted to a const char*.

-Alan

Thanks. I appreciate it.

The only reason I asked is becuase I was told in Dalnet's #c++ that this
was UB.

Thanks so much for the info.

I think I gotta find a link to the standards so I can answer my own
questions like this.
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.