String Type Conversion

There are many string types in C ++: WideString, UnicodeString, String, wstring, string, AnsiString, Variant

There are a lot of conversions in my code, for example

WideString s1 = UnicodeString ( wstring(s2.str().c_str()).c_str()).c_str();

That one word: confusing!

Is there an easy way to handle all string conversions with one helper class without thinking about how to convert one string type to another, for example:

s1 = sc(s2);    // sc = string-converter

or

sc(s1,s2);      // s1 = convert to, s2 = convert from
+4
source share
5 answers

-, . , , , . , WideString UnicodeString , wstring string. , , :

template <class TypeFrom, class TypeTo>
TypeTo convert(const TypeFrom &from)
{
    return static_cast<TypeTo>(from);
}

wstring string. :

template <>
std::string convert<std::wstring, std::string>(const std::wstring &from)
{
    return std::string(from.begin(), from.end());
}

, . , :

https://msdn.microsoft.com/en-us/library/ms235631.aspx

0

C89-C99 , . , ( ), .

#define CONV_TO_WSTRING(s1) ( wstring(s1.str().c_str()).c_str()).c_str()

.

:

s1 = CONV_TO_WSTRING(s2);

0

!

, :

string  s = convert<wstring,string> (wstring(L"hello"));

, , . ? !

std::string convert(const std::wstring &from)
{
  return std::string(from.begin(), from.end());
}

std::wstring convert(const std::string &from)
{
  return std::wstring(from.begin(), from.end());
}

:

string str2 = convert(wstring(L"hello"));

, : . . . ^^

0

This is my current solution to the conversion problem.

std::string convert1(const std::wstring &from) {
  return std::string(from.begin(), from.end()); }

std::string convert1(const UnicodeString &from) {        // String = UnicodeString
  wstring strTemp = from.c_str();
  return std::string(strTemp.begin(), strTemp.end()); }

std::string convert1(const WideString &from) {
  wstring strTemp = from.c_bstr();
  return std::string(strTemp.begin(), strTemp.end()); }


std::wstring convert2(const std::string &from) {
  return std::wstring(from.begin(), from.end()); }

std::wstring convert2(const UnicodeString &from) {
  wstring strTemp = from.c_str();
  return std::wstring(strTemp.begin(), strTemp.end()); }

std::wstring convert2(const WideString &from) {
  wstring strTemp = from.c_bstr();
  return std::wstring(strTemp.begin(), strTemp.end()); }


UnicodeString convert3(const std::string &from) {
  wstring strTemp(from.begin(), from.end());
  return UnicodeString(strTemp.c_str()); }

UnicodeString convert3(const std::wstring &from) {
  return UnicodeString(from.c_str()); }

UnicodeString convert3(const WideString &from) {
  return UnicodeString(from.c_bstr()); }


WideString convert4(const std::string &from) {
  wstring strTemp(from.begin(), from.end());
  return WideString(strTemp.c_str()); }

WideString convert4(const std::wstring &from) {
  return WideString(from.c_str()); }

WideString convert4(const UnicodeString &from) {
  return WideString(from.c_str()); }



wchar_t* convert5(const std::string &from) {
  static wstring strTemp;
  strTemp.clear();
  strTemp.assign(from.begin(), from.end());
  return (wchar_t*) strTemp.c_str(); }

wchar_t* convert5(const std::wstring &from) {
  return (wchar_t*) from.c_str(); }

wchar_t* convert5(const UnicodeString &from) {
  return from.c_str(); }

wchar_t* convert5(const WideString &from) {
  return from.c_bstr(); }

Using:

  std::string   s1  = convert1 (wstring       (L"Hallo1" ));
  std::string   s2  = convert1 (UnicodeString (L"Hallo2" ));
  std::string   s3  = convert1 (WideString    (L"Hallo3" ));
  std::wstring  s4  = convert2 (std::string   ( "Hallo4" ));
  std::wstring  s5  = convert2 (UnicodeString (L"Hallo5" ));
  std::wstring  s6  = convert2 (WideString    (L"Hallo6" ));
  UnicodeString s7  = convert3 (std::string   ( "Hallo7" ));
  UnicodeString s8  = convert3 (std::wstring  (L"Hallo8" ));
  UnicodeString s9  = convert3 (WideString    (L"Hallo9" ));
  WideString    s10 = convert4 (std::string   ( "Hallo10"));
  WideString    s11 = convert4 (std::wstring  (L"Hallo11"));
  WideString    s12 = convert4 (UnicodeString (L"Hallo12"));

  Memo->Lines->Add(convert5(s1));
  Memo->Lines->Add(convert5(s2));
  Memo->Lines->Add(convert5(s3));
  Memo->Lines->Add(convert5(s4));
  Memo->Lines->Add(convert5(s5));
  Memo->Lines->Add(convert5(s6));
  Memo->Lines->Add(convert5(s7));
  Memo->Lines->Add(convert5(s8));
  Memo->Lines->Add(convert5(s9));
  Memo->Lines->Add(convert5(s10));
  Memo->Lines->Add(convert5(s11));
  Memo->Lines->Add(convert5(s12));

Result:

Hallo1
Hallo2
Hallo3
Hallo4
Hallo5
Hallo6
Hallo7
Hallo8
Hallo9
Hallo10
Hallo11
Hallo12
0
source

Create your own template class with all possible transformations.

Inside the template, you should use type listing, e.g. static_cast

Then it only confuses once ...

EDIT

Perhaps it could be something like this:

template <typename T>
T convert(T x)
{
  T value;
  value = static_cast<T>(x);
  return value;
}

(I did not introduce an implementation because these are variable types that I don’t know. So I will try to do this)

-1
source

Source: https://habr.com/ru/post/1620509/


All Articles