Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading std::string constructor

Can I overload the std::string constructor?

I want to create a constructor which takes std::wstring and return a std::string. is it possible and how?

Thanks.

like image 599
Roee Gavirel Avatar asked Jun 08 '26 13:06

Roee Gavirel


1 Answers

Can I overload the std::string constructor?

Nope, it would require changing std::string declaration.

I want to create a constructor which takes std::wstring and return a std::string. is it possible and how?

You can have a conversion function instead, like:

std::string to_string(std::wstring const& src);

However, you need to decide what to do with symbols that can't be represented using 8-bit encoding of std::string: whether to convert them to multi-byte symbols or throw an exception. See wcsrtombs function.

like image 135
Maxim Egorushkin Avatar answered Jun 11 '26 02:06

Maxim Egorushkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!