Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      C++ named requirements:CharTraits

      From cppreference.com
      <cpp‎ |named req
       
       
      C++ named requirements
       

      CharTraits is a traits class that abstracts basic character and string operations for a given character type. Most standard library string and input/output classes require aCharTraits template type parameter alongside a corresponding character template type parameter.

      Contents

      [edit]Requirements

      No operation listed below onCharTraits may throw an exception.

      Given

      • CharT, a character type
      • X, aCharTraits type for typeCharT
      • c,d, values of typeCharT
      • p,q, values of typeconst CharT*
      • s, a value of typeCharT*
      • n,i,j, values of typestd::size_t
      • e,f, values of typeX::int_type
      • pos, a value of typeX::pos_type
      • state, a value of typeX::state_type
      • r, an lvalue of typeCharT

      [edit]Types

      TypeSemantics
      X::char_typeCharT
      X::int_type A type that can hold all valid values ofX::char_type plusX::eof()
      X::off_type Invokes implementation-defined behavior if notstd::streamoff whenX is used as the traits template parameter in input/output classes.
      X::pos_type
      • Functions in input/output classes returning this type useX::pos_type(X::off_type(-1)) as an invalid value to signal an error
      • Use of this invalid value as an argument to anystd::istream,std::ostream orstd::streambuf member taking a value of this type is undefined behavior
      • Invokes implementation-defined behavior if this type is notstd::streampos whenX is used as the traits template parameter in input/output classes
      X::state_type Destructible,CopyAssignable,CopyConstructible,DefaultConstructible

      [edit]Expressions

      ExpressionReturn typeSemantics Complexity 
      X::eq(c, d)bool Returns: whetherc is to be treated as equal tod Constant
      X::lt(c, d)bool Returns: whetherc is to be treated as less thand Constant
      X::compare(p, q, n)int Returns:
      • 0 if for eachi in[0n),X::eq(p[i], q[i]) istrue
      • Else, a negative value if
        • For somej in[0n),X::lt(p[j], q[j]) istrue and
        • For eachi in[0j),X::eq(p[i], q[i]) istrue
      • Else a positive value
      Linear
      X::length(p)std::size_t Returns: the smallesti such thatX::eq(p[i], CharT()) istrue Linear
      X::find(p, n, c)const X::char_type* Returns:
      • The smallestq in[pp+ n) such thatX::eq(*q, c) istrue
      • 0 otherwise
      Linear
      X::move(s, p, n)X::char_type*
      • For eachi in[0n), performsX::assign(s[i], p[i])
      • Copies correctly even where the ranges[pp+ n) and[ss+ n) overlap
      • Returns:s
      Linear
      X::copy(s, p, n)X::char_type*
      • Requires:[pp+ n) and[ss+ n) do not overlap
      • Returns:s
      • For eachi in[0n), performsX::assign(s[i], p[i])
      Linear
      X::assign(r, d) (Not used) Assignsr= d Constant
      X::assign(s, n, c)X::char_type*
      • For eachi in[0n), performsX::assign(s[i], c).
      • Returns:s
      Linear
      X::not_eof(e)X::int_type Returns:
      • e ifX::eq_int_type(e, X::eof()) isfalse
      • Otherwise a valuef such thatX::eq_int_type(f, X::eof()) isfalse
      Constant
      X::to_char_type(e)X::char_type Returns:
      • If for somec,X::eq_int_type(e, X::to_int_type(c)) istrue,c
      • Else some unspecified value
      Constant
      X::to_int_type(c)X::int_type Returns: some valuee, constrained by the definitions ofX::to_char_type andX::eq_int_type Constant
      X::eq_int_type(e, f)bool
      • For allc andd,X::eq(c, d) is equal toX::eq_int_type(X::to_int_type(c),
                       X::to_int_type(d))
      • Returns:
        • YieldsX::eq(c, d) if for somec andd,e== X::to_int_type(c) andf== X::to_int_type(d)
        • Otherwise, yieldstrue ife andf are both copies ofX::eof()
        • Otherwise, yieldsfalse if one ofe andf is a copy ofX::eof() and the other is not
        • Otherwise the value is unspecified
      Constant
      X::eof()X::int_type Returns: a valuee such thatX::eq_int_type(e, X::to_int_type(c)) isfalse for all valuesc Constant

      [edit]Standard library

      CharTraits is required by the following standard library class templates as a template type parameter:

      Strings
      stores and manipulates sequences of characters
      (class template)[edit]
      read-only string view
      (class template)[edit]
      Streams
      manages an arbitrary stream buffer
      (class template)[edit]
      wraps a given abstract device (std::basic_streambuf)
      and provides high-level input interface
      (class template)[edit]
      implements high-level file stream input operations
      (class template)[edit]
      implements high-level string stream input operations
      (class template)[edit]
      implements fixed character buffer input operations
      (class template)[edit]
      wraps a given abstract device (std::basic_streambuf)
      and provides high-level output interface
      (class template)[edit]
      implements high-level file stream output operations
      (class template)[edit]
      implements high-level string stream output operations
      (class template)[edit]
      synchronized output stream wrapper
      (class template)[edit]
      implements fixed character buffer output operations
      (class template)[edit]
      wraps a given abstract device (std::basic_streambuf)
      and provides high-level input/output interface
      (class template)[edit]
      implements high-level file stream input/output operations
      (class template)[edit]
      implements high-level string stream input/output operations
      (class template)[edit]
      implements fixed character buffer input/output operations
      (class template)[edit]
      Stream iterators
      input iterator that reads fromstd::basic_istream
      (class template)[edit]
      output iterator that writes tostd::basic_ostream
      (class template)[edit]
      Stream buffers
      abstracts a raw device
      (class template)[edit]
      implements raw file device
      (class template)[edit]
      implements raw string device
      (class template)[edit]
      synchronized output device wrapper
      (class template)[edit]
      implements raw fixed character buffer device
      (class template)[edit]
      Stream buffer iterators
      input iterator that reads fromstd::basic_streambuf
      (class template)[edit]
      output iterator that writes tostd::basic_streambuf
      (class template)[edit]


      CharTraits is satisfied by the following standard library explicit specializations ofstd::char_traits:

      template<>class char_traits<char>;

      template<>class char_traits<wchar_t>;
      template<>class char_traits<char8_t>;
      template<>class char_traits<char16_t>;

      template<>class char_traits<char32_t>;


      (since C++20)
      (since C++11)
      (since C++11)

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 335C++98the requirements on the binary overload of
      assign did not prevent assignments to rvalues
      its first argument
      can only be an lvalue
      LWG 352C++98X::state_type was only
      required to beCopyConstructible
      it is also required to be
      CopyAssignable andDefaultConstructible
      LWG 3085C++98X::copy(s, p, n) only requiredp not
      in[ss+ n), which is too weak[1]
      requires[pp+ n) and
      [ss+ n) not to overlap
      1. [pp+ n) and[ss+ n) can overlap, usingstd::memcpy to implementX::copy results in undefined behavior in this case.
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/named_req/CharTraits&oldid=158844"

      [8]ページ先頭

      ©2009-2025 Movatter.jp