C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

s = std::string() vs. s = ""
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Kevin Saff
Guest





PostPosted: Wed Sep 29, 2004 6:07 pm    Post subject: s = std::string() vs. s = "" Reply with quote



When should one use the default constructor for strings, versus assigning
the null literal ""? I have found that my code typically uses "" for
returns and default arguments, but I am beginning to think this is poor
style. String() seems more direct.

--
+---- Kevin C. Saff ----+ F-15 | |Eagle
Quote:
Engineer in St. Louis | _____|_^_|_____
Tracking/Fleet Support| * + [_(x)_] + *




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Howard Hinnant
Guest





PostPosted: Thu Sep 30, 2004 10:32 am    Post subject: Re: s = std::string() vs. s = "" Reply with quote



In article <I4rwAp.C94 (AT) news (DOT) boeing.com>,
"Kevin Saff" <google.com (AT) kevin (DOT) saff.net> wrote:

Quote:
When should one use the default constructor for strings, versus assigning
the null literal ""? I have found that my code typically uses "" for
returns and default arguments, but I am beginning to think this is poor
style. String() seems more direct.

sting() can be far more efficient than string(""). The former knows at
compile time to construct a zero length string. The latter must parse
the const char* at runtime with strlen and only then decide that it
doesn't need to allocate a bunch of memory and memcpy the data in.

The Metrowerks implementation of string() (Pro 9) is only 3 or 4
non-branching assembly language statements (inlined of course).
Mentally compare that with what is needed to parse a const char*, figure
out how much capacity you need, allocate it if necessary, and memcpy the
data in.

-Howard

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Fri Oct 01, 2004 11:44 am    Post subject: Re: s = std::string() vs. s = "" Reply with quote



"Kevin Saff" <google.com (AT) kevin (DOT) saff.net> wrote...
Quote:
When should one use the default constructor for strings, versus assigning
the null literal ""? I have found that my code typically uses "" for
returns and default arguments, but I am beginning to think this is poor
style. String() seems more direct.

s.clear() or s.erase() seem the most direct to me.

V


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Zian Smith
Guest





PostPosted: Fri Oct 01, 2004 11:48 am    Post subject: Re: s = std::string() vs. s = "" Reply with quote

"Kevin Saff" <google.com (AT) kevin (DOT) saff.net> wrote

Quote:
When should one use the default constructor for strings, versus assigning
the null literal ""? I have found that my code typically uses "" for
returns and default arguments, but I am beginning to think this is poor
style. String() seems more direct.


I think they both almost come down to the same thing, they both call
constructors..

s = std::string(); //calls the default constructor, string()
s = ""; //calls the overloaded constructor, string(const char*)

I guess the default constructor call is a little faster since it does
not involve passing around a parameter. If speed is a consideration
perhaps you want to use that?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
news
Guest





PostPosted: Fri Oct 01, 2004 11:52 am    Post subject: Re: s = std::string() vs. s = "" Reply with quote


"Kevin Saff" <google.com (AT) kevin (DOT) saff.net> wrote

Quote:
When should one use the default constructor for strings, versus assigning
the null literal ""? I have found that my code typically uses "" for
returns and default arguments, but I am beginning to think this is poor
style. String() seems more direct.

It is more direct, but the truth of the matter is that a zero length string fed to
the charT* constructor isn't much of a performance penalty.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bob Hairgrove
Guest





PostPosted: Fri Oct 01, 2004 11:54 am    Post subject: Re: s = std::string() vs. s = "" Reply with quote

On 29 Sep 2004 14:07:23 -0400, "Kevin Saff"
<google.com (AT) kevin (DOT) saff.net> wrote:

Quote:
When should one use the default constructor for strings, versus assigning
the null literal ""? I have found that my code typically uses "" for
returns and default arguments, but I am beginning to think this is poor
style. String() seems more direct.

When you write this:

std::string x = "";

you are actually constructing a temporary string and invoking the copy
constructor to construct x with the temporary. A good compiler would
probably help optimize away the temporary, but I like using the
default constructor better because it surely must be the "most
optimized" of all.

Whether or not it is better style ... I tend to think that any time I
see something written like the above example, it means the programmer
didn't quite trust (or know) what the default constructor is doing.
That doesn't ncessarily make a good impression, as a programmer, not
knowing the default behavior of an STL class like std::string.

--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Pete Becker
Guest





PostPosted: Fri Oct 01, 2004 3:50 pm    Post subject: Re: s = std::string() vs. s = "" Reply with quote

Howard Hinnant wrote:
Quote:

The Metrowerks implementation of string() (Pro 9) is only 3 or 4
non-branching assembly language statements (inlined of course).
Mentally compare that with what is needed to parse a const char*, figure
out how much capacity you need, allocate it if necessary, and memcpy the
data in.

if (!*str)
3 or 4 non-branching assembly language statements (inlined of course)

That's almost certainly not going to be an application's primary
bottleneck. <g>

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
news
Guest





PostPosted: Fri Oct 01, 2004 4:03 pm    Post subject: Re: s = std::string() vs. s = "" Reply with quote


"Howard Hinnant" <hinnant (AT) metrowerks (DOT) com> wrote

more efficient than string(""). The former knows at
Quote:
compile time to construct a zero length string. The latter must parse
the const char* at runtime with strlen and only then decide that it
doesn't need to allocate a bunch of memory and memcpy the data in.

It won't take strlen very long to determine the string size is zero.
If the string size is zero, the compiler isn't going to allocate memory or copy
anything.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
James Hopkin
Guest





PostPosted: Fri Oct 01, 2004 4:57 pm    Post subject: Re: s = std::string() vs. s = "" Reply with quote

"Kevin Saff" <google.com (AT) kevin (DOT) saff.net> wrote

Quote:
When should one use the default constructor for strings, versus assigning
the null literal ""? I have found that my code typically uses "" for
returns and default arguments, but I am beginning to think this is poor
style. String() seems more direct.


I would say s.clear() is the most direct incantation.


James

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ulrich Eckhardt
Guest





PostPosted: Fri Oct 01, 2004 5:11 pm    Post subject: Re: s = std::string() vs. s = "" Reply with quote

Kevin Saff wrote:
Quote:
When should one use the default constructor for strings, versus assigning
the null literal ""? I have found that my code typically uses "" for
returns and default arguments, but I am beginning to think this is poor
style. String() seems more direct.

Two comments here:
- using string() is potentially faster. Further, using clear() to clear a
string is even more expressive and potentially another bit faster.
- in the presence of generic programming, you can't use "", e.g.
template<typename CharType>
void clear_string( std::basic_string<CharType>& bar)
{
bar = ""; // fails for CharType = wchar_t
bar = std::basic_string<CharType>(); // works, but hard to read
bar.clear(); // short, fast, works.
}

Uli

--
FAQ: http://parashift.com/c++-faq-lite/

/* bittersweet C++ */
default: break;

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Kevin Saff
Guest





PostPosted: Fri Oct 01, 2004 5:16 pm    Post subject: Re: s = std::string() vs. s = "" Reply with quote

"Bob Hairgrove" <invalid (AT) bigfoot (DOT) com> wrote

Quote:
On 29 Sep 2004 14:07:23 -0400, "Kevin Saff"
[email]google.com (AT) kevin (DOT) saff.net[/email]> wrote:

When should one use the default constructor for strings, versus
assigning
the null literal ""? I have found that my code typically uses "" for
returns and default arguments, but I am beginning to think this is poor
style. String() seems more direct.

When you write this:

std::string x = "";

you are actually constructing a temporary string and invoking the copy
constructor to construct x with the temporary. A good compiler would
probably help optimize away the temporary, but I like using the
default constructor better because it surely must be the "most
optimized" of all.

Whether or not it is better style ... I tend to think that any time I
see something written like the above example, it means the programmer
didn't quite trust (or know) what the default constructor is doing.
That doesn't ncessarily make a good impression, as a programmer, not
knowing the default behavior of an STL class like std::string.

I think I've always found that line ugly. I should have given examples of
the particular cases where I have been likely to use "":

inline std::string using_null_literals (std::string const& argument =
"")
{
return "";
}

Compare this to:

inline std::string using_default_construction (std::string const&
argument = std::string())
{
return std::string();
}

(Or better, a typedef String somewhere at namespace level to get rid of the
ugly :: operator, and allow switching between different string
implementations, like wstring.)

The former is somewhat faster to type, and when using a non-empty literal
for a default argument or return there is no reason not to just use
"a-literal-string". So, I suppose I have in the past preferred "" for
consistency. I now think just std::string() is better since it allows the
binding of the construction of an empty object at compile time, rather than
testing at run-time.




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Kevin Saff
Guest





PostPosted: Fri Oct 01, 2004 5:19 pm    Post subject: Re: s = std::string() vs. s = "" Reply with quote

"Howard Hinnant" <hinnant (AT) metrowerks (DOT) com> wrote

Quote:
In article <I4rwAp.C94 (AT) news (DOT) boeing.com>,
"Kevin Saff" <google.com (AT) kevin (DOT) saff.net> wrote:

When should one use the default constructor for strings, versus
assigning
the null literal ""? I have found that my code typically uses "" for
returns and default arguments, but I am beginning to think this is poor
style. String() seems more direct.

sting() can be far more efficient than string(""). The former knows at
compile time to construct a zero length string. The latter must parse
the const char* at runtime with strlen and only then decide that it
doesn't need to allocate a bunch of memory and memcpy the data in.

The Metrowerks implementation of string() (Pro 9) is only 3 or 4
non-branching assembly language statements (inlined of course).
Mentally compare that with what is needed to parse a const char*, figure
out how much capacity you need, allocate it if necessary, and memcpy the
data in.

Thanks, I think that convinces me; since I don't see any advantage to "",
and string() might be faster, there's no good reason to use "".

I doubt I'll be able to tell the difference speed-wise in my current
project, which is limited by virtual memory thrashing due to my data storage
design - really generic, really two orders of magnitude too large for the
application. I think it'll be awhile before considerations such as "" vs.
string() show up in my performance instrumentation.




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Kevin Saff
Guest





PostPosted: Sat Oct 02, 2004 1:08 am    Post subject: Re: s = std::string() vs. s = "" Reply with quote

"James Hopkin" <jhopkin (AT) reflectionsinteractive (DOT) com> wrote

Quote:
"Kevin Saff" <google.com (AT) kevin (DOT) saff.net> wrote

When should one use the default constructor for strings, versus
assigning
the null literal ""? I have found that my code typically uses "" for
returns and default arguments, but I am beginning to think this is poor
style. String() seems more direct.


I would say s.clear() is the most direct incantation.

I'm sorry, I meant constructing, not assigning. I agree s.clear() is better
than either s = string() or s = "".




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
news
Guest





PostPosted: Tue Oct 05, 2004 6:17 am    Post subject: Re: s = std::string() vs. s = "" Reply with quote


"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote

Quote:
"Kevin Saff" <google.com (AT) kevin (DOT) saff.net> wrote...
When should one use the default constructor for strings, versus assigning
the null literal ""? I have found that my code typically uses "" for
returns and default arguments, but I am beginning to think this is poor
style. String() seems more direct.

s.clear() or s.erase() seem the most direct to me.

It would if he was trying to erase an string. What he is trying to do is
pass an empty string to another function.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Tue Oct 05, 2004 10:05 pm    Post subject: Re: s = std::string() vs. s = "" Reply with quote

Howard Hinnant wrote:
Quote:
In article <I4rwAp.C94 (AT) news (DOT) boeing.com>,
"Kevin Saff" <google.com (AT) kevin (DOT) saff.net> wrote:

When should one use the default constructor for strings, versus assigning
the null literal ""? I have found that my code typically uses "" for
returns and default arguments, but I am beginning to think this is poor
style. String() seems more direct.

sting() can be far more efficient than string(""). The former knows at
compile time to construct a zero length string. The latter must parse
the const char* at runtime with strlen and only then decide that it
doesn't need to allocate a bunch of memory and memcpy the data in.
snip


VC++ 7.1 can evaluate strlen(string-literal) at compile-time. You
need to work harder on your optimiser. ;-)

--
Ben Hutchings
Nothing is ever a complete failure; it can always serve as a bad example.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.