Archive 17/01/2023.

Cannot cast std::string to Urho3D String

TheGreatMonkey

I’m working with a plugin that isn’t compatible with Urho3D strings

so I have need to cast from std::string into String

so something like this should work:

std::string foo = “I"m a string”;
String bar = String(foo);

however during compilation I get this error:
‘ToString’: is not a member of 'std::basic_string<cahr_traits>,std::allocator>

the error throws against line 145 of str.h

DavidHT

Use the non-modifiable standard C character array version of the std::string:
String bar(foo.c_str());

TheGreatMonkey

Yep, that does the trick. Thanks!

S.L.C

In addition to information given by @DavidHT you could use a more explicit constructor that also receives the size and thus avoid a strlen operation on the given string.

String bar(foo.c_str(), foo.size()); //<- Might get a warning about truncated integer types on x64 since std::string::size yields a size_t value and Urho3D::String uses unsigned int. Just a heads up.
Eugene

Btw I never understood complains about redundant strlen in such places.
You are already copying this string, you have to traverse N bytes to copy it. For small strings within few cache lines one extra data traversal doesn’t change anything.

S.L.C

I guess you are right about that. I stand corrected.