Re: basic_string ctor



Duane Hebert wrote:
"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx> wrote in message
news:eUe10xwWFHA.2768@xxxxxxxxxxxxxxxxxxxxxxx

John wrote:

Hi,

Is it legal C++ (by which I mean according to the standard spec) to
pass a null into a basic_string ctor?  Or, more precisely, is it
legal C++ to pass a null pointer into a basic_string parameter on a
method?

No, it's not. The committee was worried about the cost of the extra if() in the constructor being too high (misplaced premature optimization, IMO).


In my opinion the cost of the extra if() would have been well worth
it.  One of the problems with the string.h functions was the need to
check everything (if(ptr)(if strlen(prt)) //do something with ptr)

Having functions that take std::string and give expected results
with null would have been great.  I've heard from people involved
with the standard that this may be changed.

What behaviour should we have for passing null? An exception thrown? Certainly I don't particularly like the idea of treating null identically to "", since they aren't the same thing.


Tom
.



Relevant Pages

  • Re: The difference between 0 and NULL?
    ... I noticed there existed only one ctor for the class and it was ... It's a no argument ctor that initializes 3 variables in ... I noticed that the pointer was being ... Perhaps your first compile was bad ...
    (comp.unix.programmer)
  • RE: ECMA Wrong - Class and Object Initialization Rules - Help!
    ... > An instance of a class can be created only if the constructor of the class' ... the this pointer is initialized. ... In my example, the .ctor of Object is not invoked at all - so, strictly ... the two .ctor-s invoke each other. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: The difference between 0 and NULL?
    ... > program (the calls to printf() notwithstanding) and ModemObject is a ... I noticed there existed only one ctor for the class and it was ... It's a no argument ctor that initializes 3 variables in ... I noticed that the pointer was being ...
    (comp.unix.programmer)
  • Re: why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers
    ... > object when I store a pointer to the object. ... using namespace std; ... The 2nd ctor is for the *copy* that is pushed into 'lists'. ... The first dtor is for the X erased by erase). ...
    (comp.lang.cpp)
  • Re: Function in header file - newbie VC++/CLI
    ... and that doesn't work in any language. ... "Duane Hebert" wrote in message ... You don't really need to expressly use the this pointer anyway since it is implied where it can be used. ... use of "String ^Message" I wasn't sure if the same rules ...
    (microsoft.public.vc.language)

Loading