+ 1

Why?

char d[4] = {'G','o','o','d'}; std::cout<<static_cast<std::string>(d); // NO ERROR BUT... char c = 'C'; std::cout<<static_cast<std::string>(c); // ERROR

8th Jan 2019, 9:47 AM
Lucifer
Lucifer - avatar
9 Respostas
+ 9
~ swim ~ Kinshuk Vasisht Dennis Thanks big time for given insights, as for me, I guess the reference link posted by Kinshuk had shown me a bit more reasoning as to why the casting didn't succeed. Dennis' experiment dug up something new about string, something I never knew : ) AZTECCO Welcome and Thanks for stopping by : ) C++ Soldier (Babak) Thanks for sparing time, sorry to bother : ) Sorry to reply with edits, I didn't want to sound chatting ; )
8th Jan 2019, 2:34 PM
Ipang
+ 9
Ipang thanks for consideration! 💘 ~ swim ~ great answer as usual 👍
8th Jan 2019, 2:56 PM
AZTECCO
AZTECCO - avatar
+ 8
Yeah, sorry, let's see what the masters has to say ; ) AZTECCO C++ Soldier (Babak) Dennis Kinshuk Vasisht KrOW Shadow ~ swim ~ @Hatsy Rei (Idk why I can't mention) Help please ...
8th Jan 2019, 10:01 AM
Ipang
+ 7
From en.cppreference.com/w/cpp/language/static_cast, point 1 reads: "...static_cast<new_type>(expression) returns the imaginary variable Temp initialized as if by new_type Temp(expression);, which may involve implicit conversions, a call to the constructor of new_type or a call to a user-defined conversion operator. ..." Thus the compiler resorted to std::string(expression), when the implicit conversion failed. Since no method or function exists that can help perform this explicit conversion, like a single char argument constructor in the string class or the () operator accepting a single char, the operation fails and the compiler raises an error.
8th Jan 2019, 1:05 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
While I was shoveling the soil, I came up with a more intuitive way of looking at the issue of 'c' guy: This 'c' variable is of `char` type. A `char type is one of the integral types. To convert from an integral type to `string` what we usually are going to do? We probably use `to_string()` function. Let's see the result: char c = 'C'; cout << to_string(c); // 67 But, that's not what we were looking for. The reason for getting an integer result is because `to_string()` lacks an overloaded version for `char` arguments. It converts the 'c' guy silently to `int` and produces the string representation of that `int`. It doesn't solve OP's problem but it was a good finding while doing hard labor! 8D
8th Jan 2019, 3:05 PM
Babak
Babak - avatar
+ 4
Can't explain the reason, but to initialize a std::string from a single char you can use the constructor; char x {'C'}; std::string sample(1, x); Hth, cmiiw
8th Jan 2019, 9:58 AM
Ipang
+ 4
Just a little experiment here. It is possible to cast a char array to an std::string with reinterpret_cast, without a call to the constructor like static_cast, but it requires a bit more work and you need to understand the underlying bits of an std::string. Since this is implementation defined, this is not portable. https://code.sololearn.com/cBySwzB4gIY7/ The output of the code is a bunch of numbers, the important numbers in this case are the 9th ( starting count from 1 ) and the 17th. The 9th character represents the size of the string The 17th character represents the array itself, but only when the string is using the short string optimization, notice that if you do something like t.reserve( 500 ) this array changes into the size of the reserved string. Therefore this method only works with short strings, you'd probably have to find the pointer if you want to do this with longer strings. So all we do if overwrite the short character array and the size of the string.
8th Jan 2019, 2:09 PM
Dennis
Dennis - avatar
+ 4
~ swim ~ Yeah, but manipulating the underlying bytes is much more fun than a simple assignment. :)
8th Jan 2019, 2:39 PM
Dennis
Dennis - avatar
+ 1
Its all about conversion..dude , I can also do that..
8th Jan 2019, 10:00 AM
Lucifer
Lucifer - avatar