2

Hello I start studying c++ and I'm trying to learn what data type is something that starts with a L, like

auto something = L"Monday";

What data type is auto? And how do I convert a std::string to this data type?

std::string my_string = "Hello World"; to ? something = L"Hello World"

  • https://learn.microsoft.com/en-us/cpp/cpp/string-and-character-literals-cpp?view=msvc-160 – santahopar Nov 14 '20 at 03:07
  • Thx, so L"" is a wchar_t*, is possible to convert a string to a wchar_t*? –  Nov 14 '20 at 03:09
  • It's a [wide string literal](https://en.cppreference.com/w/cpp/language/string_literal). There's a few ways discussed [here](https://stackoverflow.com/questions/246806/i-want-to-convert-stdstring-into-a-const-wchar-t) to widen a `std::string` into a `std::wstring`. – Nathan Pierson Nov 14 '20 at 03:09
  • 2
    `L""` is not a `wchar_t*`. It's a `const wchar_t[1]`, and decays to a `const wchar_t*` in most contexts. Sometimes, the difference is crucial. – Deduplicator Nov 14 '20 at 03:17

1 Answers1

7

auto is not a data-type. It is a placeholder that gets deduced depending on the initializer used.

In your case, the initializer is a wide string-literal of type const wchar_t[size], which decays to const wchar_t* when used to initialize the variable.

A wide string can be stored in a std::wstring.

How to convert a std::string (a narrow string) to a wide string depends on the source's character encoding.

Anyway, there are many others who asked that too:

C++ Convert string (or char*) to wstring (or wchar_t*)

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Deduplicator
  • 44,692
  • 7
  • 66
  • 118