Questions tagged [char-traits]

Character traits classes specify character properties and provide specific semantics for certain operations on characters and sequences of characters.

Character traits classes specify character properties and provide specific semantics for certain operations on characters and sequences of characters.

The standard library includes a standard set of character traits classes that can be instantiated from the char_traits template, and which are used by default both for the basic_string objects and for the input/output stream objects.

17 questions
90
votes
1 answer

What is the point of STL Character Traits?

I notice that in my copy of the SGI STL reference, there is a page about Character Traits but I can't see how these are used? Do they replace the string.h functions? They don't seem to be used by std::string, e.g. the length() method on std::string…
Matthew Smith
  • 6,165
  • 6
  • 34
  • 35
14
votes
1 answer

Why does this specialized char_traits and codecvt for use with the basic_ifstream template throw std::bad_cast?

There are already questions here on Stackoverflow asking why basic_fstream doesn't work. The answers say that char_traits is only specialized for char and wchar_t (plus char16_t, char32_t in C++11) and you should stick with…
x-x
  • 7,287
  • 9
  • 51
  • 78
14
votes
3 answers

What's the purpose of std::char_traits::assign()?

void assign(char_type& to, char_type from); Why can't you just use the assignment operator instead of using this function? What is this used for?
user541686
  • 205,094
  • 128
  • 528
  • 886
4
votes
1 answer

Is it guaranteed that std::char_traits::to_int_type(c) == static_cast(c)?

The question How to use correctly the return value from std::cin.get() and std::cin.peek()? made me wonder if it is guaranteed that std::char_traits::to_int_type(c) == static_cast(c) for all valid char values c. This comes up in a lot…
L. F.
  • 19,445
  • 8
  • 48
  • 82
3
votes
3 answers

removing constexpr from a variable capturing a constexpr function return value removes compile-time evaluation

Consider the following constexpr function, static_strcmp, which uses C++17's constexpr char_traits::compare function: #include constexpr bool static_strcmp(char const *a, char const *b) { return std::char_traits::compare(a, b, …
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
3
votes
2 answers

Leading/trailing whitespace insensitive traits for basic_string

I am doing a lot of parsing/processing, where leading/trailing whitespace and case insensitivity is given. So I made a basic char trait for std::basic_string(see below) to save myself some work. The trait is not working, the problem is that…
user8705939
2
votes
1 answer

Is it possible to use std::char_traits in constant expressions before GCC 8?

According to the GCC 8 Release Note, it is now possible to use std::char_traits and std::char_traits in constant expressions: Improved experimental support for C++17, including the following features: std::char_traits and…
Pierre
  • 1,942
  • 3
  • 23
  • 43
1
vote
2 answers

Why std::basic_fstream won't work?

When trying to compile this code: std::fstream file("file.name", std::ios::out | std::ios::binary); uint8_t buf[BUFSIZE]; //Fill the buffer, etc... file.write(buf, BUFSIZE); compiler will give me warning about oh-not-so-healthy conversion from…
PookyFan
  • 785
  • 1
  • 8
  • 23
1
vote
0 answers

VS 2015 std::char_traits operations

At my workplace, we changed string type (which holds internationalized characters) for from std::wstring to std::u16string after VS 2015(Update 3) compiler upgrade. Due to this, we are seeing loads of performance regressions such as this. The…
Recker
  • 1,915
  • 25
  • 55
1
vote
0 answers

How can I use char_traits algorithms with a custom iterator type?

I'd like to be able to use functions like std::char_traits<...>::length, compare, etc., but pass a custom iterator type instead of a pointer. Is this possible? And if not, is there a reason why this isn't supported in the standard?
Aaron
  • 594
  • 2
  • 12
1
vote
1 answer

How to read cstrings from char**?

I have a char** (a null terminate array: last element of array is NULL) returned by a function (which I can not change): char** ptar = get_ptar(); I want to iterate over it and pass its value to another function (again which I can not…
Annie
  • 3,090
  • 9
  • 36
  • 74
0
votes
1 answer

Why does std::basic_string have two separate template parameters _Elem (char type) and _Traits (char traits)?

The problem is I don't understand why those should be separate. Why not use one class, like CharType, that would contain both the logic of char traits and char type. I mean replace that: template ,…
Capy Maths
  • 79
  • 6
0
votes
1 answer

How to assign a std::string to std::basic_string(Unicode2String) on Linux

I am working on a Linux system, and I think standard Linux std::string supports both Unicode and ASCII characters. So, I want to use std::string in my code, but I receive strings from an application in the format of std::basic_string
0
votes
2 answers

C++ std::basic_string/char_traits specialization

This is related to: std::basic_string specialization and Circumventing template specialization I tried the solution from std::basic_string specialization, but the problem is that CustomChar is a typedef for wchar_t and I have redefinition(conflict…
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
0
votes
1 answer

How one can safely serialize std::basic_istream::pos_type?

In one of my projects I have to cache positional information about certain data chunks found in large files. I've already implemented a small API built around std::basic_istream::pos_type placed in maps. Now I need to serialize these…
Crank
  • 55
  • 1
  • 7
1
2