mercredi 3 mai 2017

Is size of char_traits

Consider the following program:

#include <iostream>
#include <sstream>
#include <string>

int main(int, char **) {
  std::basic_stringstream<char16_t> stream;

  stream.put(u'\u0100');
  std::cout << " Bad: " << stream.bad() << std::endl;

  stream.put(u'\uFFFE');
  std::cout << " Bad: " << stream.bad() << std::endl;

  stream.put(u'\uFFFF');
  std::cout << " Bad: " << stream.bad() << std::endl;

  return 0;
}

The output is:

 Bad: 0                                                                                                                                                                                
 Bad: 0                                                                                                                                                                                
 Bad: 1  

It seems the reason the badbit gets set is because 'put' sets the badbit if the character equals std::char_traits::eof(). I can now no longer put to the stream.

At http://ift.tt/2pGf6gy it states:

int_type: an integer type that can hold all values of char_type plus EOF

But if char_type is the same as int_type (uint_least16_t) then how can this be true?

Aucun commentaire:

Enregistrer un commentaire