Re: basic_string ctor
- From: "Jeff F" <not@xxxxxxxxxxxx>
- Date: Thu, 19 May 2005 09:15:10 -0400
"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
.
- References:
- basic_string ctor
- From: John
- RE: basic_string ctor
- From: John
- Re: basic_string ctor
- From: Stephen Howe
- Re: basic_string ctor
- From: Duane Hebert
- Re: basic_string ctor
- From: Stephen Howe
- basic_string ctor
- Prev by Date: Re: basic_string ctor
- Next by Date: Re: basic_string ctor
- Previous by thread: Re: basic_string ctor
- Next by thread: vector<bool> iterator bug in VC8 ?
- Index(es):
Relevant Pages
|