Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      C++ Standard Library

      From cppreference.com
      <cpp
       
       
      Standard library
       

      The C++ standard library provides a wide range of facilities that are usable in standard C++.

      Contents

      [edit]Category

      Thelanguage support library provides components that are required by certain parts of the C++ language, such as memory allocation (new/delete) andexception processing.

      Theconcepts library describes library components that C++ programs may use to perform compile-time validation oftemplate arguments and perform function dispatch based on properties of types.

      (since C++20)

      Thediagnostics library provides a consistent framework for reporting errors in a C++ program, includingpredefined exception classes.

      Thememory management library provides components for memory management, includingsmart pointers andscoped allocator(since C++11).

      Themetaprogramming library describes facilities for use intemplates and during constant evaluation, includingtype traits,integer sequence,(since C++14) andrational arithmetic.

      (since C++11)

      Thegeneral utilities library includes components used by other library elements, such as apredefined storage allocator for dynamic storage management, and components used as infrastructure in C++ programs, such astuples and(since C++11)function wrappers.

      Thecontainers,iterators,ranges(since C++20), andalgorithms libraries provide a C++ program with access to a subset of the most widely used algorithms and data structures.

      Thestrings library provides support for manipulating text represented as homogeneous sequences of following types:char,char8_t(since C++20),char16_t,char32_t(since C++11),wchar_t, and any other character-like types.

      Thetext processing library providesregular expression matching and searching(since C++11), utilities fortext formatting(since C++20),identifying text encodings(since C++26) andlocalization facilities.

      Thenumerics library providesnumeric algorithms andcomplex number components that extend support for numeric processing. Thevalarray component provides support for n-at-a-time processing, potentially implemented as parallel operations on platforms that support such processing. Therandom number component provides facilities for generating pseudo-random numbers.(since C++11)

      Thetime library provides generally useful time utilities.

      Theinput/output library provides theiostream components that are the primary mechanism for C++ program input and output. They can be used with other elements of the library, particularly strings, locales, and iterators.

      Thethread support library provides components to create and manage threads, includingatomic operations,mutual exclusion, and inter-thread communication.

      (since C++11)

      Theexecution support library provides a framework for managing asynchronous execution on generic execution resources.

      (since C++26)

      [edit]Library contents

      The C++ standard library provides definitions for theentities andmacros described in the synopses of theC++ standard library headers, unless otherwise specified.

      All library entities exceptoperator new andoperator delete are defined within the namespacestd ornamespaces nested within namespacestd (except the entities for the C standard library facilities, see below). It is unspecified whether names declared in a specific namespace are declared directly in that namespace or in aninline namespace inside that namespace.(since C++11)

      [edit]Headers

      Each element of the C++ standard library is declared or defined (as appropriate) in aheader. A header is not necessarily a source file, nor are the sequences delimited by< and> in header names necessarily valid source file names.

      The C++ standard library provides theC++ library headers andadditional C++ headers for C library facilities (see “headers” page for descriptions):

      C++ library headers
      <algorithm><iomanip><list><ostream><streambuf>
      <bitset><ios><locale><queue><string>
      <complex><iosfwd><map><set><typeinfo>
      <deque><iostream><memory><sstream><utility>
      <exception><istream><new><stack><valarray>
      <fstream><iterator><numeric><stdexcept><vector>
      <functional><limits>
      Headers added in C++11
      <array><condition_variable><mutex><scoped_allocator><type_traits>
      <atomic><forward_list><random><system_error><typeindex>
      <chrono><future><ratio><thread><unordered_map>
      <codecvt><initializer_list><regex><tuple><unordered_set>
      Headers added in C++14
      <shared_mutex>
      Headers added in C++17
      <any><execution><memory_resource><string_view><variant>
      <charconv><filesystem><optional>
      Headers added in C++20
      <barrier><concepts><latch><semaphore><stop_token>
      <bit><coroutine><numbers><source_location><syncstream>
      <compare><format><ranges><span><version>
      Headers added in C++23
      <expected><flat_set><mdspan><spanstream><stdfloat>
      <flat_map><generator><print><stacktrace>
      Headers added in C++26
      <contracts><hazard_pointer><inplace_vector><rcu><text_encoding>
      <debugging><hive><linalg><simd>
      Removed headers
      <codecvt>(since C++11)(deprecated in C++17)(removed in C++26)
      <strstream>(deprecated in C++98)(removed in C++26)
      C++ headers for C library facilities
      <cassert><clocale><cstdarg><cstring>
      <cctype><cmath><cstddef><ctime>
      <cerrno><csetjmp><cstdio><cwchar>
      <cfloat><csignal><cstdlib><cwctype>
      <climits>
      Headers added in C++11
      <cfenv><cinttypes><cstdint><cuchar>
      Removed headers
      <ccomplex>(since C++11)(deprecated in C++17)(removed in C++20)
      <ciso646>(removed in C++20)
      <cstdalign>(since C++11)(deprecated in C++17)(removed in C++20)
      <cstdbool>(since C++11)(deprecated in C++17)(removed in C++20)
      <ctgmath>(since C++11)(deprecated in C++17)(removed in C++20)

      Afreestanding implementation has an implementation-defined set of headers, seehere for the minimal requirement on the set of headers.

      [edit]C standard library

      The C++ standard library also makes available the facilities of the C standard library, suitably adjusted to ensure static type safety. The descriptions of many library functions rely on the C standard library for the semantics of those functions.

      In some cases, the signatures specified in standard C++ may be different from the signatures in the C standard library, and additional overloads may be declared, but the behavior and the preconditions(including those implied by C'srestrict)(since C++17) are the same unless otherwise stated.

      For compatibility with the C standard library, the C++ standard library provides the C headers listed below. The intended use of these headers is for interoperability only. It is possible that C++ source files need to include one of these headers in order to be valid ISO C. Source files that are not intended to also be valid ISO C should not use any of the C headers. Seehere for descriptions.

      C headers
      <assert.h><limits.h><stdarg.h><string.h>
      <ctype.h><locale.h><stddef.h><time.h>
      <errno.h><math.h><stdio.h><wchar.h>
      <float.h><setjmp.h><stdlib.h><wctype.h>
      <iso646.h><signal.h>
      Headers added in C++11
      <complex.h><inttypes.h><stdbool.h><tgmath.h>
      <fenv.h><stdalign.h><stdint.h><uchar.h>
      Headers added in C++23
      <stdatomic.h>
      Headers added in C++26
      <stdbit.h><stdchkint.h>

      Except otherwise noted, the contents of each headercxxx is the same as that of the corresponding headerxxx.h as specified in theC standard library. In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope of the namespacestd. It is unspecified whether these names (including any overloads added) are first declared within the global namespace scope and are then injected into namespacestd by explicitusing-declarations.

      Names which are defined as macros in C (assert,offsetof,setjmp,va_arg,va_end andva_start) must be defined as macros in the C++ standard library, even if C grants license for implementation as functions.

      Names that are defined as functions in C must be defined as functions in the C++ standard library. This disallows the practice, allowed in C, of providing a masking macro in addition to the function prototype. The only way to achieve equivalent inline behavior in C++ is to provide a definition as an externinline function.

      Identifiers that are keywords or operators in C++ cannot be defined as macros in C++ standard library headers. In particular, including the standard header<iso646.h> has no effect.

      [edit]Names associated with safe functions in standard C(since C++17)

      If any C++ header is included, it is implementation-defined whether any of the following C standard Annex K names is declared in the global namespace (none of them is declared in namespacestd):

      C standard Annex K names
      abort_handler_smbstowcs_sstrncat_svswscanf_s
      asctime_smemcpy_sstrncpy_svwprintf_s
      bsearch_smemmove_sstrtok_svwscanf_s
      constraint_handler_tmemset_sswprintf_swcrtomb_s
      ctime_sprintf_sswscanf_swcscat_s
      errno_tqsort_stmpfile_swcscpy_s
      fopen_sRSIZE_MAXTMP_MAX_Swcsncat_s
      fprintf_srsize_ttmpnam_swcsncpy_s
      freopen_sscanf_svfprintf_swcsnlen_s
      fscanf_sset_constraint_handler_svfscanf_swcsrtombs_s
      fwprintf_ssnprintf_svfwprintf_swcstok_s
      fwscanf_ssnwprintf_svfwscanf_swcstombs_s
      gets_ssscanf_svprintf_swmemcpy_s
      gmtime_smbstowcs_svscanf_svswscanf_s
      abort_handler_sstrcat_svsnprintf_swmemmove
      ignore_handler_sstrcpy_svsnwprintf_swprintf_s
      localtime_sstrerrorlen_svsprintf_swscanf_s
      L_tmpnam_sstrerror_svsscanf_s
      mbsrtowcs_sstrlen_svswprintf_s

      [edit]Using the library

      [edit]Including headers

      The entities in the C++ standard library are defined in headers, whose contents are made available to a translation unit when it contains the appropriate#include preprocessing directive.

      A translation unit may include library headers in any order. Each may be included more than once, with no effect different from being included exactly once, except that the effect of including either<cassert> or<assert.h> depends each time on the lexically current definition ofNDEBUG.

      A translation unit can only include a header outside of any declaration or definition, and lexically before the first reference in that translation unit to any of the entities declared in that header. No diagnostic is required.

      Inmodule units, headers can only be included inglobal module fragments.

      (since C++20)

      Importing headers

      TheC++ library headers, or, for a freestanding implementation, the subset of such headers that are provided by the implementation, are collectively known as theimportable C++ library headers.

      The contents of importable C++ library headers are made available to a translation unit when it contains the appropriateimport declaration.

      (since C++20)

      Importing modules

      The C++ standard library provides the followingC++ library modules:

      • Thenamed modulestd exports declarations in namespacestd that are provided by the importable C++ library headers (e.g.std::rotr from<bit>) and theC++ headers for C library facilities (e.g.std::puts from<cstdio>). It additionally exports declarations in the global namespace for the storageallocation anddeallocation functions that are provided by<new> (e.g.::operator new).
      • The named modulestd.compat exports the same declarations as the named modulestd, and additionally exports declarations in the global namespace corresponding to the declarations in namespacestd that are provided by the C++ headers for C library facilities (e.g.::fclose).

      For each declaration in the standard library,

      • the module itattaches to is unspecified, and
      • it denotes the sameentity regardless of whether it was made reachable through including a header, importing a header unit, or importing a C++ library module.
      (since C++23)

      [edit]Linkage

      Entities in the C++ standard library havestorage duration#external linkage. Unless otherwise specified, objects and functions have the defaultextern"C++"linkage.

      Whether a name from the C standard library declared with external linkage hasextern"C" orextern"C++" linkage is implementation-defined. The C++ standard recommends usingextern"C++" in this case.

      Objects and functions defined in the library and required by a C++ program are included in the program prior to program startup.

      [edit]Requirements on standard library implementations

      [edit]Guarantees

      A C++ header must providedeclarations anddefinitions that appear in

      • the synopsis of that header, or
      • the synopsis of another header which is appeared to be included in the synopsis of that header.

      For types and macros defined in multiple headers (such asNULL), including any number of these headers in any order never violates theone definition rule.

      Unless otherwise specified, allobject-like macros defined by the C standard library that expand to integralconstant expressions can be used in#if preprocessing directives.

      Calling a standard library non-member function signature always results in actually calling that function. Therefore a conforming standard library implementation cannot define additional non-member functions that may be called by a valid C++ program.

      Non-member function signatures are never declared with additionaldefault arguments.

      Unless otherwise specified, calls made by functions in the standard library to non-operator, non-member functions do not use functions from anothernamespace which are found throughargument-dependent name lookup.

      For eachfriend declaration of a function (template) within a class (template) definition, no other declaration is provided for that function (template).

      Standard library function signatures can only be declared asconstexpr if they are required to beconstexpr (libstdc++ cmathis notably non-conforming here). If a header provides any non-defining declarations of constexpr functions or constructors, the corresponding definitions should also be provided within that header.

      Unless otherwise specified, each standard library function should meet each of the following requirements to preventdata races:

      • A C++ standard library function cannot (directly or indirectly) access objects accessible by threads other than the current thread unless the objects are accessed (directly or indirectly) via the function’s arguments, includingthis.
      • A C++ standard library function cannot (directly or indirectly) modify objects accessible by threads other than the current thread unless the objects are accessed (directly or indirectly) via the function’s non-const arguments, includingthis.
        • For example, an object with static storage duration cannot be used for internal purposes without synchronization because doing so can cause a data race even in programs that do not explicitly share objects between threads.
      • A C++ standard library function cannot access objects indirectly accessible via its arguments or via elements of itscontainer arguments except by invoking functions required by its specification on those container elements.
      • An operation oniterators obtained by calling a standard library container or string member function can access, but not modify, the underlying container.
        • In particular, container operations that invalidate iterators conflict with operations on iterators associated with that container.
      • A C++ standard library function can only perform all operations solely within the current thread if those operations have effects that arevisible to users.
        • Operations without visible side effects can be parallelized.
      (since C++11)

      For each class defined in the C++ standard library required to bederived from another class defined in the C++ standard library,

      • the base class must bevirtual if it is specified asvirtual,
      • the base class cannot be virtual if it is not specified asvirtual, and
      • unless otherwise specified, types with distinct names shall be distinct types.

      Unless otherwise specified, all types specified in the C++ standard library are non-final types.

      (since C++11)

      If a function defined in the C++ standard library is specified to throw anexception (in a particular situation) of a given type, the exception thrown can only have that type or a type derived from that type so that an exception handler for the base type can catch it.

      Functions from the C standard library can only throw exceptions when such a function calls a program-supplied function that throws an exception (qsort() andbsearch() meet this condition).

      Destructor operations defined in the C++ standard library never throw exceptions. Every destructor in the C++ standard library behaves as if it had anon-throwing exception specification.

      If a function in the C++ standard library report errors via astd::error_code object, that object'scategory() member must returnstd::system_category() for errors originating from the operating system, or a reference to an implementation-definedstd::error_category object for errors originating elsewhere. The possible values ofvalue() for each of these error categories should be defined.

      Objects of types defined in the C++ standard library may bemoved from. Move operations can either be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects will be placed in a valid but unspecified state.

      An object of a type defined in the C++ standard library may bemove-assigned to itself. Unless otherwise specified, such an assignment places the object in a valid but unspecified state.

      (since C++11)

      [edit]Implementation freedom

      It is unspecified whether any member or non-member functions in the C++ standard library are defined asinline.

      For a non-virtual C++ standard library member function, a different set of member function signatures can be declared, provided that any call to that member function that would select an overload from the given set of declarations behaves as if that overload was selected. This allows, for instance:

      • adding parameters with default arguments,
      • replacing a member function with default arguments with two or more member functions with equivalent behavior, or
      • adding additional signatures for a member function name.

      Unless otherwise specified, it is implementation-defined which functions in the C++ standard library may be recursively reentered.

      C++ standard library implementations can share their own internal objects between threads if the objects are not visible to users and are protected against data races.

      (since C++11)

      It is unspecified whether any function signature or class in the C++ standard library is a friend of another class in the C++ standard library.

      The names and global function signatures describedhere are reserved to the implementation.

      Any class in the C++ standard library can be derived from a class with a name reserved to the implementation. If a class defined in the C++ standard library is required to be derived from other classes in the C++ standard library, that class can be derived directly from the required base or indirectly through a hierarchy of base classes with names reserved to the implementation.

      If a function defined in the C++ standard library is not specified to throw an exception but does not have a non-throwing exception specification, the exception thrown is implementation-defined, but its type should bestd::exception or any type derived fromstd::exception.

      The exception specification for a non-virtual function can be strengthened by adding a non-throwing exception specification.

      Standard library hardening

      An implementation can be ahardened implementation , whether the implementation is hardened is implementation-defined.

      Some standard library member functions (and member function templates) havehardened precondition . When such a function is invoked:

      • If the implementation is hardened, prior to any other observable side effects of the function, one or morecontract assertions whose predicates are as described in the hardened precondition are evaluated with a checking semantic. If any of these assertions is evaluated with a non-terminating semantic and thecontract-violation handler returns, the behavior is undefined.
      • If the implementation is not hardened, when a hardened precondition is violated, the behavior is undefined.

      Member functions with hardened preconditions

      (since C++26)

      [edit]Notes

      libstdc++,libc++, andSTL all support using standard library modules in C++20 mode.

      Feature-test macroValueStdFeature
      __cpp_lib_modules202207L(C++23)Standard library modulesstd andstd.compat
      Hardened implementation only
      __cpp_lib_hardened_array202502L(C++26)Hardenedstd::array
      __cpp_lib_hardened_basic_string202502L(C++26)Hardenedstd::basic_string
      __cpp_lib_hardened_basic_string_view202502L(C++26)Hardenedstd::basic_string_view
      __cpp_lib_hardened_bitset202502L(C++26)Hardenedstd::bitset
      __cpp_lib_hardened_deque202502L(C++26)Hardenedstd::deque
      __cpp_lib_hardened_expected202502L(C++26)Hardenedstd::expected
      __cpp_lib_hardened_forward_list202502L(C++26)Hardenedstd::forward_list
      __cpp_lib_hardened_inplace_vector202502L(C++26)Hardenedstd::inplace_vector
      __cpp_lib_hardened_list202502L(C++26)Hardenedstd::list
      __cpp_lib_hardened_mdspan202502L(C++26)Hardenedstd::mdspan
      __cpp_lib_hardened_optional202502L(C++26)Hardenedstd::optional
      __cpp_lib_hardened_span202502L(C++26)Hardenedstd::span
      __cpp_lib_hardened_valarray202502L(C++26)Hardenedstd::valarray
      __cpp_lib_hardened_vector202502L(C++26)Hardenedstd::vector

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 1C++98the language linkages of the names from
      the C standard library were unspecified
      they are
      implementation-defined
      LWG 119C++98the exception specifications of virtual
      functions could be strengthened
      only allowed for
      non-virtual functions
      LWG 147C++98the specification on non-member
      functions only considered global functions
      also considers
      non-global functions
      LWG 225C++98standard library functions might call non-member functions
      from other namespaces due to argument-dependent lookup
      prohibited unless
      otherwise specified
      LWG 336C++98<strstream> was not a C++ library headerit is a C++ library header
      LWG 343C++98library header dependencies were not specifiedspecified (listed in synopses)
      LWG 456C++98C++ headers for C library facilities could
      only provide definitions in namespacestd
      allowed to define in global namespace
      and then inject into namespacestd
      LWG 465C++98identifiers that are keywords or operators in C++ could
      be defined as macros in C++ standard library headers
      (only<ciso646> is required not to define them as macros)
      all C++ standard
      library headers cannot
      define them as macros
      LWG 1178C++98C++ headers must include a C++ header
      that contains any needed definition
      C++ headers must provide declarations
      and definitions that are directly or
      indirectly included in its synopsis
      LWG 2013C++11it was unspecified whether the functions not
      required by the standard to be constexpr can
      be declaredconstexpr by the standard library
      prohibited
      LWG 2225C++98a diagnostic was required if a header
      is included at an incorrect position
      no diagnostic is
      required in this case

      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/standard_library&oldid=183167"

      [8]ページ先頭

      ©2009-2025 Movatter.jp