Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/types"

From cppreference.com
< cpp
(Miscellaneous transformations: +)
m (See also: added link to type traits.)
 
(26 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{title|Type support (basic types, RTTI, type traits)}}
+
{{title|Type support (basic types, RTTI)}}
 
{{cpp/types/navbar}}
 
{{cpp/types/navbar}}
  
<small>(See also {{ltt|cpp/language/type}} for type system overview)</small>
+
See also [[cpp/language/type|type system overview]] and [[cpp/language/types|fundamental types defined by the language]].
===Basic types===
+
  
====[[cpp/language/types | Fundamental types defined by the language]]====
+
===Additional basic types and macros===
 
+
====Additional basic types and macros====
+
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | cstddef}}
+
{{dsc header|cstddef}}
{{dsc inc | cpp/types/dsc size_t}}
+
{{dsc inc|cpp/types/dsc size_t}}
{{dsc inc | cpp/types/dsc ptrdiff_t}}
+
{{dsc inc|cpp/types/dsc ptrdiff_t}}
{{dsc inc | cpp/types/dsc nullptr_t}}
+
{{dsc inc|cpp/types/dsc nullptr_t}}
{{dsc inc | cpp/types/dsc NULL}}
+
{{dsc inc|cpp/types/dsc NULL}}
{{dsc inc | cpp/types/dsc max_align_t}}
+
{{dsc inc|cpp/types/dsc max_align_t}}
{{dsc inc | cpp/types/dsc offsetof}}
+
{{dsc inc|cpp/types/dsc offsetof}}
{{dsc inc | cpp/types/dsc byte}}
+
{{dsc inc|cpp/types/dsc byte}}
 +
{{dsc break}}
 
{{dsc sep}}
 
{{dsc sep}}
{{dsc header | cstdbool | {{mark until c++20}} }}
+
{{dsc header|cstdbool|{{mark deprecated}} {{mark until c++20}}}}
{{dsc header | stdbool.h}}
+
{{dsc header|stdbool.h}}
{{dsc macro const | __bool_true_false_are_defined | nolink=true | C compatibility macro constant, expands to integer constant {{c|1}}|notes={{mark c++11}}}}
+
{{dsc inc|cpp/types/dsc bool_true_false_are_defined}}
 
{{dsc sep}}
 
{{dsc sep}}
{{dsc header | cstdalign| {{mark until c++20}} }}
+
{{dsc header|cstdalign|{{mark deprecated}} {{mark until c++20}}}}
{{dsc header | stdalign.h}}
+
{{dsc header|stdalign.h}}
{{dsc macro const | __alignas_is_defined | nolink=true | C compatibility macro constant, expands to integer constant {{c|1}}|notes={{mark c++11}}}}
+
{{dsc inc|cpp/types/dsc alignas_is_defined}}
 +
{{dsc inc|cpp/types/dsc alignof_is_defined}}
 
{{dsc end}}
 
{{dsc end}}
  
====[[cpp/types/integer | Fixed width integer types]] {{mark since c++11}}====
+
===[[cpp/types/integer|Fixed width integer types]] {{mark since c++11}}===
 +
 
 +
===[[cpp/types/floating-point|Fixed width floating-point types]] {{mark since c++23}}===
  
 
===Numeric limits===
 
===Numeric limits===
 
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | limits}}
+
{{dsc header|limits}}
{{dsc inc | cpp/types/dsc numeric_limits}}
+
{{dsc inc|cpp/types/dsc numeric_limits}}
 
{{dsc end}}
 
{{dsc end}}
  
Line 39: Line 39:
  
 
===Runtime type identification===
 
===Runtime type identification===
 
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | typeinfo}}
+
{{dsc header|typeinfo}}
{{dsc inc | cpp/types/dsc type_info}}
+
{{dsc inc|cpp/types/dsc type_info}}
{{dsc inc | cpp/types/dsc bad_typeid}}
+
{{dsc inc|cpp/types/dsc bad_typeid}}
{{dsc inc | cpp/types/dsc bad_cast}}
+
{{dsc inc|cpp/types/dsc bad_cast}}
 
{{dsc sep}}
 
{{dsc sep}}
{{dsc header | typeindex}}
+
{{dsc header|typeindex}}
{{dsc inc | cpp/types/dsc type_index}}
+
{{dsc inc|cpp/types/dsc type_index}}
 
{{dsc end}}
 
{{dsc end}}
  
===Type traits {{mark since c++11}}===
+
===Defect reports===
 
+
{{dr list begin}}
Type traits defines a compile-time template-based interface to query or modify the properties of types.
+
{{dr list item|wg=lwg|dr=4036|std=C++11|before={{tt|__alignof_is_defined}} was underspecified in the C++ standard|after=specified and deprecated}}
 
+
{{dr list end}}
Attempting to specialize a template defined in the {{tt|<type_traits>}} header results in undefined behavior, except that {{lc|std::common_type}} may be specialized [[cpp/types/common_type#Specializations|as described in its description]].
+
 
+
A template defined in the {{tt|<type_traits>}} header may be instantiated with an incomplete type unless otherwise specified, notwithstanding the general prohibition against instantiating standard library templates with incomplete types.
+
 
+
====Type properties====
+
 
+
{{dsc begin}}
+
{{dsc header | type_traits}}
+
{{dsc h2 | Primary type categories}}
+
{{dsc inc | cpp/types/dsc is_void}}
+
{{dsc inc | cpp/types/dsc is_null_pointer}}
+
{{dsc inc | cpp/types/dsc is_integral}}
+
{{dsc inc | cpp/types/dsc is_floating_point}}
+
{{dsc inc | cpp/types/dsc is_array}}
+
{{dsc inc | cpp/types/dsc is_enum}}
+
{{dsc inc | cpp/types/dsc is_union}}
+
{{dsc inc | cpp/types/dsc is_class}}
+
{{dsc inc | cpp/types/dsc is_function}}
+
{{dsc inc | cpp/types/dsc is_pointer}}
+
{{dsc inc | cpp/types/dsc is_lvalue_reference}}
+
{{dsc inc | cpp/types/dsc is_rvalue_reference}}
+
{{dsc inc | cpp/types/dsc is_member_object_pointer}}
+
{{dsc inc | cpp/types/dsc is_member_function_pointer}}
+
 
+
{{dsc h2 | Composite type categories}}
+
{{dsc inc | cpp/types/dsc is_fundamental}}
+
{{dsc inc | cpp/types/dsc is_arithmetic}}
+
{{dsc inc | cpp/types/dsc is_scalar}}
+
{{dsc inc | cpp/types/dsc is_object}}
+
{{dsc inc | cpp/types/dsc is_compound}}
+
{{dsc inc | cpp/types/dsc is_reference}}
+
{{dsc inc | cpp/types/dsc is_member_pointer}}
+
 
+
{{dsc h2 | Type properties}}
+
{{dsc inc | cpp/types/dsc is_const}}
+
{{dsc inc | cpp/types/dsc is_volatile}}
+
{{dsc inc | cpp/types/dsc is_trivial}}
+
{{dsc inc | cpp/types/dsc is_trivially_copyable}}
+
{{dsc inc | cpp/types/dsc is_standard_layout}}
+
{{dsc inc | cpp/types/dsc is_pod}}
+
{{dsc inc | cpp/types/dsc is_literal_type}}
+
{{dsc inc | cpp/types/dsc has_unique_object_representations}}
+
{{dsc inc | cpp/types/dsc is_empty}}
+
{{dsc inc | cpp/types/dsc is_polymorphic}}
+
{{dsc inc | cpp/types/dsc is_abstract}}
+
{{dsc inc | cpp/types/dsc is_final}}
+
{{dsc inc | cpp/types/dsc is_aggregate}}
+
{{dsc inc | cpp/types/dsc is_signed}}
+
{{dsc inc | cpp/types/dsc is_unsigned}}
+
{{dsc end}}
+
 
+
{{dsc begin}}
+
{{dsc h2 | Supported operations}}
+
{{dsc inc | cpp/types/dsc is_constructible}}
+
{{dsc inc | cpp/types/dsc is_default_constructible}}
+
{{dsc inc | cpp/types/dsc is_copy_constructible}}
+
{{dsc inc | cpp/types/dsc is_move_constructible}}
+
{{dsc inc | cpp/types/dsc is_assignable}}
+
{{dsc inc | cpp/types/dsc is_copy_assignable}}
+
{{dsc inc | cpp/types/dsc is_move_assignable}}
+
{{dsc inc | cpp/types/dsc is_destructible}}
+
{{dsc inc | cpp/types/dsc has_virtual_destructor}}
+
{{dsc inc | cpp/types/dsc is_swappable}}
+
{{dsc end}}
+
 
+
{{dsc begin}}
+
{{dsc h2 | Property queries}}
+
{{dsc inc | cpp/types/dsc alignment_of}}
+
{{dsc inc | cpp/types/dsc rank}}
+
{{dsc inc | cpp/types/dsc extent}}
+
{{dsc end}}
+
 
+
{{dsc begin}}
+
{{dsc h2 | Type relationships}}
+
{{dsc inc | cpp/types/dsc is_same}}
+
{{dsc inc | cpp/types/dsc is_base_of}}
+
{{dsc inc | cpp/types/dsc is_convertible}}
+
{{dsc inc | cpp/types/dsc is_invocable}}
+
{{dsc end}}
+
 
+
====Type modifications====
+
 
+
Type modification templates create new type definitions by applying modifications on a template parameter. The resulting type can then be accessed through {{tt|type}} member typedef.
+
 
+
{{dsc begin}}
+
{{dsc header | type_traits}}
+
{{dsc h2 | Const-volatility specifiers}}
+
{{dsc inc | cpp/types/dsc remove_cv}}
+
{{dsc inc | cpp/types/dsc add_cv}}
+
 
+
{{dsc h2 | References}}
+
{{dsc inc | cpp/types/dsc remove_reference}}
+
{{dsc inc | cpp/types/dsc add_reference}}
+
 
+
{{dsc h2 | Pointers}}
+
{{dsc inc | cpp/types/dsc remove_pointer}}
+
{{dsc inc | cpp/types/dsc add_pointer}}
+
 
+
{{dsc h2 | Sign modifiers}}
+
{{dsc inc | cpp/types/dsc make_signed}}
+
{{dsc inc | cpp/types/dsc make_unsigned}}
+
 
+
{{dsc h2 | Arrays}}
+
{{dsc inc | cpp/types/dsc remove_extent}}
+
{{dsc inc | cpp/types/dsc remove_all_extents}}
+
{{dsc end}}
+
 
+
====Miscellaneous transformations====
+
 
+
{{dsc begin}}
+
{{dsc header | type_traits}}
+
{{dsc inc | cpp/types/dsc aligned_storage}}
+
{{dsc inc | cpp/types/dsc aligned_union}}
+
{{dsc inc | cpp/types/dsc decay}}
+
{{dsc inc | cpp/types/dsc remove_cvref}}
+
{{dsc inc | cpp/types/dsc enable_if}}
+
{{dsc inc | cpp/types/dsc conditional}}
+
{{dsc inc | cpp/types/dsc common_type}}
+
{{dsc inc | cpp/types/dsc common_reference}}
+
{{dsc inc | cpp/types/dsc underlying_type}}
+
{{dsc inc | cpp/types/dsc result_of}}
+
{{dsc inc | cpp/types/dsc void_t}}
+
{{dsc inc | cpp/types/dsc type_identity}}
+
{{dsc end}}
+
 
+
====Operations on traits====
+
{{dsc begin}}
+
{{dsc header | type_traits}}
+
{{dsc inc | cpp/types/dsc conjunction}}
+
{{dsc inc | cpp/types/dsc disjunction}}
+
{{dsc inc | cpp/types/dsc negation}}
+
{{dsc end}}
+
 
+
====Helper classes====
+
 
+
{{dsc begin}}
+
{{dsc header | type_traits}}
+
{{dsc inc | cpp/types/dsc integral_constant}}
+
{{dsc end}}
+
 
+
Two specializations of {{lc|std::integral_constant}} for the type {{c|bool}} are provided:
+
 
+
{{dsc begin}}
+
{{dsc header | type_traits}}
+
{{dsc hitem | Type | Definition}}
+
{{dsc | {{tt|true_type}} | {{c|std::integral_constant<bool, true>}}}}
+
{{dsc | {{tt|false_type}} | {{c|std::integral_constant<bool, false>}}}}
+
{{dsc end}}
+
 
+
===Endian {{mark since c++20}}===
+
{{dsc begin}}
+
{{dsc header | type_traits}}
+
{{dsc hitem | Type | Definition}}
+
{{dsc inc | cpp/types/dsc endian}}
+
{{dsc end}}
+
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc see c | c/types | Type support library}}
+
{{dsc|[[cpp/meta#Type traits|Type traits and metaprogramming library]]}}
 +
{{dsc see c|c/types|Type support library|nomono=true}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 07:49, 9 April 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Type support
 

See also type system overview and fundamental types defined by the language.

Contents

[edit] Additional basic types and macros

Defined in header <cstddef>
unsigned integer type returned by the sizeof operator
(typedef) [edit]
signed integer type returned when subtracting two pointers
(typedef) [edit]
(C++11)
the type of the null pointer literal nullptr
(typedef) [edit]
implementation-defined null pointer constant
(macro constant) [edit]
trivial type with alignment requirement as great as any other scalar type
(typedef) [edit]
byte offset from the beginning of a standard-layout type to specified member
(function macro) [edit]
(C++17)
the byte type
(enum) [edit]
 
Defined in header <cstdbool>(deprecated) (until C++20)
Defined in header <stdbool.h>
__bool_true_false_are_defined
(C++11)(deprecated)
C compatibility macro constant, expands to integer literal 1
(macro constant) [edit]
 
Defined in header <cstdalign>(deprecated) (until C++20)
Defined in header <stdalign.h>
__alignas_is_defined
(C++11)(deprecated)
C compatibility macro constant, expands to integer literal 1
(macro constant) [edit]
__alignof_is_defined
(C++11)(deprecated)
C compatibility macro constant, expands to integer literal 1
(macro constant) [edit]

[edit] Fixed width integer types (since C++11)

[edit] Fixed width floating-point types (since C++23)

[edit] Numeric limits

Defined in header <limits>
provides an interface to query properties of all fundamental numeric types
(class template) [edit]

[edit] C numeric limits interface

[edit] Runtime type identification

Defined in header <typeinfo>
contains some type’s information, the class returned by the typeid operator
(class) [edit]
exception that is thrown if an argument in a typeid expression is null
(class) [edit]
exception that is thrown by an invalid dynamic_cast expression, i.e. a cast of reference type fails
(class) [edit]
 
Defined in header <typeindex>
wrapper around a type_info object, that can be used as index in associative and unordered associative containers
(class) [edit]

[edit] Defect reports

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

DR Applied to Behavior as published Correct behavior
LWG 4036 C++11 __alignof_is_defined was underspecified in the C++ standard specified and deprecated

[edit] See also

Type traits and metaprogramming library
C documentation for Type support library