473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

#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
9 3270
* 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
>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
"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
* 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
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
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
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
* 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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
24
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include...
3
by: Heiko Hund | last post by:
Hi, I do not understand the deeper reason for the following compiler error $ g++ test.cpp test.cpp: In function `int main()': test.cpp:41: error: `std::basic_string<char,...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
6
by: Khuong Dinh Pham | last post by:
How do i read the contents of a xml file and output it to a std::string. Thx in advance
8
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
10
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char*...
6
by: SteelSide | last post by:
Ive searched and searched,but havent been able to get it to work. ----------------- #include <iostream> using namespace std; std::string tempHostNameStr = "s1e2.hidden.thingy.org"; char...
13
by: arnuld | last post by:
/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include <iostream> #include...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.