New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SL.str: char* vs wchar_t* vs std::string #829
Comments
There's F.25: Use a zstring or a not_null to designate a C-style string which can be seen in action in many examples in the guldelines, such as F.22: Use T* or owner<T*> to designate a single object. It is also mentioned again in a couple places, at least in R.2: In interfaces, use raw pointers to denote individual objects (only) I'd say the guidance is
perhaps it should be made more explicit, such as by filling out the rule placeholder SL.str: String As for guidelines for the use of wchar_t/char32_t/etc, that's a whole other discussion |
I'm not sure if I understood it quite right. By owned, do you mean that the called function is responsible for the parameter?
It is a lot of information that is missing in SL.str: String :-) |
In the case of #include <string>
#include <iostream>
int main() {
std::wstring data = "This is an wide string.\n";
// Note: with wide strings you need std::wcout instead of std::cout
std::wcout << data;
return 0;
} That example above has an issue on top of not being able to compile because wstring does not like #include <string>
#include <iostream>
int main() {
std::wstring data = L"This is an wide string.\n";
// Note: with wide strings you need std::wcout instead of std::cout
std::wcout << data;
return 0;
} And now it whould work. Hope this explains not only the difference between |
If I understand correctly, you're asking for compilers to accept invalid code and magically transform an array of |
Oops, sorry, I closed this but meant to just add a reply to the previous comment. Reopening. |
Ok, updated the comment. |
not really, if you're on an OS that supports Unicode, such as Linux, this works just as well:
live demo http://melpon.org/wandbox/permlink/P0LUKyLzTs1xPyKu .. but getting into that discussion would detail this thread thoroughly. |
@cubbimrw: Actually, this is not a question of Unicode support in general, but UTF-8 encoding in particular. |
@cubbimew Not all systems support unicode (like you said) and Windows, by default is set to a code page and in that not all characters are supported on it. And those characters are Japanese and Chinese characters with the default code page Windows has set up (unless you reset it) Some systems however on the local is set to UTF-8. And point being not all programs are coded to automatically translate all the text on itself pased on what the code page or the local is set to. And sometmes it is just not logical for things like small Console applications. Windows does support Unicode but only if you set the code page to be able to support UTF-8 (If they even put in a way to explicitly set it to UTF-8). |
Please stop mixing up unicode and UTF-8. Unicode is a standard that uniquely maps all? Known characters (actually code points) to a number. UTF-8 (usually using The problem is that afaik linux - by default - assumes that a char* points to a utf-8 encoded string, whereas a windows assumes by default that it is some single byte encoding like latin-1, which can only encode a small subset. wchar_t afaik always assumed to be a unicode code unit on both platforms, but has different sizes and encodings (2 byte, utf-16 encoding on windows, 4 byte, utf-32 encoding on linux) |
I did not use a
It's UCS2 on Windows, obsolete as of 1996:
|
Bjarne, per our meeting, please write this up during a minute slice of your infinite spare time. |
I have made a start on the ASCII-string part of this |
@BjarneStroustrup |
I see that the ASCII part is there now, but what about the Unicode part? |
I can not understand from the guide when I should use
char*
and when I should usestd::string
in parameters and return value.In most of the examples you use
char*
, but is not that little too imprecise?What about the difference between
char*
andwchar_t*
?The text was updated successfully, but these errors were encountered: