Reputation: 20264
Is std::string
supposed to hold a set of characters in Ascii encoding on all platforms and standard compilers?
In other words, can I be sure that my C++ program will get a set of Ascii characters if I do this:
std::string input;
std::getline(std::cin, input);
EDIT:
In more accurate words, I want to make sure that if the user enter "a0"
I will get a std::string
with two elements. The first one is 97
and the second is 48
Upvotes: 5
Views: 6539
Reputation: 1
In other words, can I be sure that my C++ program will get a set of Ascii characters if I do this ...
No. std::string
is actually a specialization for std::basic_string<>
, like
using std::string std::basic_string<char>;
:
template<
class CharT,
class Traits = std::char_traits<CharT>,
class Allocator = std::allocator<CharT>
> class basic_string;
and can hold any type of character that is defined with Traits
.
In short std::string
can contain ASCII character encodings, as well as EBCDIC, or any others. But it should be transparent as how you're using it.
Upvotes: 3
Reputation: 385144
No. std::string
does not hold "characters"; it holds bytes.
Those bytes could form some human-readable string through encoding as ASCII or EDBCIC or Unicode. They could be a binary encoding storing computer-readable information (e.g. a JPEG image). They could be guidelines from aliens on how to use Stack Overflow for three weeks straight without being downvoted even once. They could be total random white noise.
Your program needs to be made to understand what the data it is reading actually means, and how it is encoded. This shall be part of your task as the programmer.
(It's unfortunate, and nowadays misleading, that char
is named char
.)
Upvotes: 10
Reputation: 206577
No, there is no guarantee that
std::string input;
std::getline(std::cin, input);
will return only ASCII characters. The range of values that be held by a char
is not limited to the ASCII characters.
If your platform uses different encoding than ASCII, you'll obviously get a different set of characters.
Even if your platform uses ASCII encoding, if char
on the platform is an unsigned type, than it can very easily hold the extended ASCII characters too.
Upvotes: 3