1

I don't know why this is not compiling.

std::wstring number = std::to_wstring((long long)another_number);

Compiler : gcc 5.1.0

IDE : codeblocks 17.12

phuclv
  • 32,499
  • 12
  • 130
  • 417
Praveen Kumar
  • 143
  • 1
  • 12

2 Answers2

1

you have to ensure that:

you have included the string header:

#include <string>

you are compiling with c++11 flag: -std=c++11

$ g++ -std=c++11 your_file.cpp -o your_program

here is the official doc https://en.cppreference.com/w/cpp/string/basic_string/to_wstring

----and ofcourse, i hope you mean something like

 std::wstring number = std::to_wstring((long long)anotherNumber);

instead of

std::wstring number = std::to_wstring((long long)number);

coz you cant declare number and initialize it with a another variable named number...

this example here is working fine:

#include <iostream>
#include <string>

int main() {
    auto numberX{2020};
    std::wstring f_str = std::to_wstring((long long)numberX);
    std::wcout << f_str;
    
    return 0;
}
ΦXocę 웃 Пepeúpa ツ
  • 45,713
  • 17
  • 64
  • 91
  • In GCC 5.4.0 [the default standard version is `-std=gnu++98`](https://stackoverflow.com/a/44057210/995714) so I guess it's really the reason – phuclv Jun 24 '21 at 08:35
0

Workaround

std::string temp = std::to_string((long long)number);
std::wstring number_w(temp.begin(), temp.end());
Praveen Kumar
  • 143
  • 1
  • 12
  • GCC 5.1 rejects this (correctly). `long long(number)` has to be spelled as `(long long)number` (since `long long` is spelled with more than one word). – HolyBlackCat Feb 05 '22 at 09:21
  • @HolyBlackCat this works fine in visual studio – Praveen Kumar Feb 05 '22 at 09:42
  • Visual studio is wrong, see (2) [here](https://en.cppreference.com/w/cpp/language/explicit_cast). And (modern) VS should support `to_wstring` without needing a workaround... – HolyBlackCat Feb 05 '22 at 09:46