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

J

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. 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).
 
A

Alf P. Steinbach

* 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.
 
S

Son of Sam

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?
 
J

Jim Langston

Son of Sam said:
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.
 
A

Alf P. Steinbach

* Jim Langston:
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?
 
S

Steven T. Hatton

Jim said:
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.
 
A

Alan Johnson

Jim said:
#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
 
A

Alf P. Steinbach

* Rolf Magnus:
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?
 
J

Jim Langston

Alan Johnson said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top