std::char_t...ts::length,(3)
π std::char_traits::length π
okay so welcome to the std::char_traits::length page from the C++ Standard Libary manual β yes, thatβs the manual name right up top, and we are absolutely doing this. This one is short, cute, and very βjust read the null terminator, bestie.β
π NAME π
The NAME situation is truly a choice, and it shows up literally like this:
std::char_traits::length,std::char_traits::length, -
std::char_traits::length,std::char_traits::length,
Anyway: this page is about std::char_traits::length, and yes, itβs giving βthe same thing said twice with commas.β Iconic in a chaotic way.
π¦ Synopsis π¦
Hereβs the signature moment, literally:
std::char_traits<char32_t>::length
static std::size_t length( const char_type* s ); (constexpr since C++17)
So it returns the length of the character sequence pointed to by s, which is the position of the terminating null character (char_type()). No cap.
Also, for the general requirements on character traits for X::length, see CharTraits. Thatβs right there in the original, so donβt come for me.
πΈ Parameters πΈ
sβ pointer to a character sequence; this is the sequence whose length youβre getting back. Thatβs the whole assignment, bestie.
π Return value π
The length of the character sequence pointed to by s. Periodt.
β¨ Complexity β¨
Linear. No sneaky shortcuts, no magical constant-time glow-up. Just linear, fr fr.
π Example π
Run this code and watch it do its lil thing:
// Run this code
#include <iomanip>
#include <iostream>
#include <string>
void print(const char* str)
{
std::cout << std::quoted(str) << " has length = "
<< std::char_traits<char>::length(str) << '\n';
}
int main()
{
print("foo");
std::string s{"booo"};
print(s.c_str());
}
π Output π
"foo" has length = 3
"booo" has length = 4
Source/version: http://cppreference.com, dated 2024.06.10, page tag std::char_t...ts::length,(3). byeee, go count your characters responsibly πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms