Re: basic_string ctor

Tech-Archive recommends: Speed Up your PC by fixing your registry




"Stephen Howe" <stephenPOINThoweATtns-globalPOINTcom> wrote in message
news:%23gILBBHXFHA.160@xxxxxxxxxxxxxxxxxxxxxxx
>> The problem is
>>
>> void foo(std::string flarn) {
>> // anything at all
>> }
>>
>> With this type of function a char*
>> can be passed and std::string's ctor
>> will be called. You would have to
>> provide char* versions to use your
>> suggestion.
>
> Nonsense.
> This works for me on compiling with cl /GX /O1 nulltest.cpp:
> Your foo works without providing any char * versions.
>
>>>>>>>>>>>>>>>>>>
> #include <string>
> #include <iostream>
> using namespace std;
>
> void foo(std::string flarn)
> { cout << '[' << flarn << "]\n";
> }
>
> int main()
> {
> char *p = "Hello";
> char *q = "";
> char *r = 0;
>
> foo(p);
> foo(q);
> foo(r != 0 ? r : "");
>
> return 0;
> }

And the above can be easily codified with:

std::string empty_if_null( const char* p){ return p? p : ""; }

...

f( empty_if_null(r) );

Which is nicely self documenting in those cases where a null check is
needed, and is easy to find with an editor.

Jeff Flinn


.



Relevant Pages

  • Re: How to redesign big switch case?
    ... > I wrote an editor that reads chars, and does a switch to process ... if char is HOME then go to the beginning of the ... it would be nice to allow users to derive their own editor class ... virtual void onKey ...
    (comp.lang.cpp)
  • How to redesign big switch case?
    ... I wrote an editor that reads chars, and does a switch to process the ... from my one and add support for more special keys. ...
    (comp.lang.cpp)
  • Re: How to redesign big switch case?
    ... > I wrote an editor that reads chars, and does a switch to process ... if char is HOME then go to the beginning of the ... it would be nice to allow users to derive their own editor class ... > from my one and add support for more special keys. ...
    (comp.lang.cpp)
  • Re: How to redesign big switch case?
    ... > I wrote an editor that reads chars, and does a switch to process the ... if char is HOME then go to the beginning of the ... > from my one and add support for more special keys. ...
    (comp.lang.cpp)
  • SSPI Kerberos for delegation
    ... const char *tokenSource, const char *name = NULL, ... DWORD bufsiz = sizeof buf; ... int n = ib.cbBuffer; ... // wserr() displays winsock errors and aborts. ...
    (microsoft.public.dotnet.framework.remoting)