Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      C++ named requirements:AllocatorAwareContainer(since C++11)

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

      AnAllocatorAwareContainer is aContainer that holds an instance of anAllocator and uses that instance in all its member functions to allocate and deallocate memory and to construct and destroy objects in that memory (such objects may be container elements, nodes, or, for unordered containers, bucket arrays), except thatstd::basic_string specializations do not use the allocators for construction/destruction of their elements(since C++23).

      The following rules apply to container construction:

      • Copy constructors ofAllocatorAwareContainers obtain their instances of the allocator by callingstd::allocator_traits<allocator_type>::select_on_container_copy_construction on the allocator of the container being copied.
      • Move constructors obtain their instances of allocators by move-constructing from the allocator belonging to the old container.
      • All other constructors take aconst allocator_type& parameter.

      The only way to replace an allocator is copy-assignment, move-assignment, and swap:

      • Copy-assignment will replace the allocator only ifstd::allocator_traits<allocator_type>::propagate_on_container_copy_assignment::value istrue.
      • Move-assignment will replace the allocator only ifstd::allocator_traits<allocator_type>::propagate_on_container_move_assignment::value istrue.
      • Swap will replace the allocator only ifstd::allocator_traits<allocator_type>::propagate_on_container_swap::value istrue. Specifically, it will exchange the allocator instances through an unqualified call to the non-member function swap, seeSwappable. If swap does not propagate the allocator, swapping two containers with unequal allocators is undefined behavior.
      • The accessorget_allocator() obtains a copy of the allocator that was used to construct the container or installed by the most recent allocator replacement operation.

      The only exception isstd::basic_string<CharT,Traits,Allocator>::assign, which may also propagate the allocator.

      Contents

      [edit]Requirements

      A type satisfiesAllocatorAwareContainer if it satisfiesContainer and, given the following types and values, the semantic and complexity requirements in the tables below are satisfied:

      Type Definition
      X anAllocatorAwareContainer type
      T thevalue_type ofX
      A the allocator type used byX
      Value Definition
      a,b non-const lvalues of typeX
      c an lvalue of typeconst X
      t an lvalue or a const rvalue of typeX
      rv a non-const rvalue of typeX
      m a value of typeA

      [edit]Types

      Name Type Requirement
      typename X::allocator_type AX::allocator_type::value_type andX::value_type are the same.

      [edit]Statements

      StatementSemanticsComplexity
      X u;
      X u= X();
      PreconditionA isDefaultConstructible.Constant
      Postcondition u.empty() andu.get_allocator()== A() are bothtrue.
      X u(m);Postconditionu.empty() andu.get_allocator()== m are bothtrue.Constant
      X u(t, m);PreconditionT isCopyInsertable intoX.Linear
      Postconditionu== t andu.get_allocator()== m are bothtrue.
      X u(rv);Postcondition
      • u has the same elements asrv had before this construction.
      • The value ofu.get_allocator() is the same as the value ofrv.get_allocator() before this construction.
      Constant
      X u(rv, m);PreconditionT isMoveInsertable intoX.
      • Constant ifm== rv.get_allocator() istrue.
      • Otherwise linear.
      Postcondition
      • u has the same elements, or copies of the elements, thatrv had before this construction.
      • u.get_allocator()== m istrue.

      [edit]Expressions

      Expression Type Semantics Complexity 
      c.get_allocator()ANo direct semantic requirement.Constant
      a= tX&PreconditionT isCopyInsertable intoX andCopyAssignable.Linear
      Postcondition a== t istrue.
      a= rvX&PreconditionIf the allocator willnot be replaced by move-assignment (seeabove), thenT isMoveInsertable intoX andMoveAssignable.Linear
      EffectAll existing elements ofa are either move assigned to or destroyed.
      PostconditionIfa andrv do not refer the same object,a is equal to the value thatrv had before the assignment.
      a.swap(b)voidEffectExchanges the contents ofa andb.Constant

      [edit]Notes

      AllocatorAwareContainers always callstd::allocator_traits<A>::construct(m, p, args) to construct an object of typeT atp usingargs, withm== get_allocator().The defaultconstruct instd::allocator calls::new((void*)p) T(args)(until C++20)std::allocator has noconstruct member andstd::construct_at(p, args) is called when constructing elements(since C++20), but specialized allocators may choose a different definition.

      [edit]Standard library

      All standard library string types and containers (exceptstd::array andstd::inplace_vector) areAllocatorAwareContainers:

      stores and manipulates sequences of characters
      (class template)[edit]
      double-ended queue
      (class template)[edit]
      singly-linked list
      (class template)[edit]
      doubly-linked list
      (class template)[edit]
      resizable contiguous array
      (class template)[edit]
      collection of key-value pairs, sorted by keys, keys are unique
      (class template)[edit]
      collection of key-value pairs, sorted by keys
      (class template)[edit]
      collection of unique keys, sorted by keys
      (class template)[edit]
      collection of keys, sorted by keys
      (class template)[edit]
      collection of key-value pairs, hashed by keys, keys are unique
      (class template)[edit]
      collection of key-value pairs, hashed by keys
      (class template)[edit]
      collection of unique keys, hashed by keys
      (class template)[edit]
      collection of keys, hashed by keys
      (class template)[edit]

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2839C++11self move assignment of standard containers was not allowedallowed but the result is unspecified
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/named_req/AllocatorAwareContainer&oldid=183359"

      [8]ページ先頭

      ©2009-2025 Movatter.jp