Link to home
Start Free TrialLog in
Avatar of pillmill
pillmill

asked on

cast char array to string ?

How do you cast a char array to a string ?

I need to use a char array output as an argument that must be a string.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or probably :

        std::string str = std::string(s);
well, std::string has implicit constructor from char*, so wherever You need std:string You can pass char*. It will be casted(converted) on-the-fly by compiler.
But it have some drawback, every time You do that, the whole char* is copied in memory.
>>Or probably :
>>        std::string str = std::string(s);

I think you mean...

std::string & str = std::string(s);

Otherwise you take an unnecessary copy, resulting in an additional heap allocation.
> std::string & str = std::string(s);
Keeping reference to temporary object is rather bad value, really!

I heard the following to work, but only for consts

string foo();

string const & s = foo();//works even foo() returns temporary
>> I think you mean...

I did mean it without the reference - the reason I posted the second post was to make clear(er) that it's an std::string constructor that is called (don't know if I succeeded). The extra copy should be optimized away by the compiler anyway.
>> Keeping reference to temporary object is rather bad value, really!
The C++ standard guarantees the temporary will live for the lifetime of the reference to which it is bound, this is perfectly safe and valid C++ code.

"The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference except as specified below. A temporary bound to a reference member in a constructors ctor-initializer (12.6.2) persists until the constructor exits. A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call. A temporary bound to the returned value in a function return statement (6.6.3) persists until the function exits. In all these cases, the temporaries created during the evaluation of the expression initializing the reference, except the temporary to which the reference is bound, are destroyed at the end of the full-expression in which they are created and in the reverse order of the completion of their construction. If the lifetime of two or more temporaries to which references are bound ends at the same point, these temporaries are destroyed at that point in the reverse order of the completion of their construction. In addition, the destruction of temporaries bound to references shall take into account the ordering of destruction of objects with static or automatic storage duration (3.7.1, 3.7.2); that is, if obj1 is an object with static or automatic storage duration created before the temporary is created, the temporary shall be destroyed before obj1 is destroyed; if obj2 is an object with static or automatic storage duration created after the temporary is created, the temporary shall be destroyed after obj2 is destroyed."

>>  heard the following to work, but only for consts
I think you are referring to passing temporaries to functions via in-place construction, whereas the function parameter should be a const reference.

foo(bar()); // The param of foo should be a const reference.

>> The extra copy should be optimized away by the compiler anyway
It depends on the compiler and is not guarenteed. The copy isn't necessary and is guaranteed not to happen if you take a reference.
Well, my gcc casts error on
std::string & a = std::string("abc");

but is fine on
std::string const & a = std::string("abc");

It makes sense in fact - You can't have non-const reference to temporary.
>> You can't have non-const reference to temporary
Although not strictly ISO compliant a lot of compilers, such as Visual Studio, allow a non-const reference to be taken to a temporary. I agree gcc does not and, in this respect, it is more compliant with the standard. That aside, it is still perfectly valid to bind a temporary to a reference and it will live for the duration of that reference.
>>>> it is still perfectly valid to bind a temporary to a reference
As there are a lot of not compliant compilers around in the wide world, someone who reads that may perfectly die when following the advice ...

>>>> std::string a = std::string("abc");

Is there any advantage over

  std::string a("abc");

(beside it wasn't compiling on gcc) ??
std::string a = std::string("abc");
std::string b("abc");

compilers are free to optimize the first call to look like second(no operator= is called), except that operator= have to be accessible.
gcc surely does the optimization. msvc as well.
>> As there are a lot of not compliant compilers around in the wide world
It'll either be supported and work or it won't will and fail to build.

>>Is there any advantage over
>>  std::string a("abc");

I think you've missed the point... I was questioning whether I8 means to assign a temporary to a value or a reference... I wasn't advocating either over normal construction!


>> compilers are free to optimize
Is not the same as they will!

I see little point in continuing this as (a) it's largely off topic and (b) was done with the other day, no?
>>>> I see little point in continuing this as (a) it's largely off topic
Bingo. Looking for the culprit ....
>> Bingo. Looking for the culprit ....
Grow up!