C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- std::result_of, std::invoke_result
- std::integral_constant
- std::is_constant_evaluated
- std::max_align_t
- offsetof
- NULL
- std::bad_typeid
- std::bad_cast
- std::numeric_limits
- std::type_info
- std::ptrdiff_t
- std::byte
- std::conjunction
- std::disjunction
- std::negation
- std::is_swappable_with, std::is_swappable, std::is_nothrow_swappable_with, std::is_nothrow_swappable
- std::is_invocable, std::is_invocable_r, std::is_nothrow_invocable, std::is_nothrow_invocable_r
- std::is_aggregate
- std::has_unique_object_representations
- std::endian
- std::remove_cvref
- std::type_index
- C æ°å¼æéæ¥å£
- å®å®½æ´æ°ç±»å (C++11 èµ·)
- std::is_bounded_array
- std::is_unbounded_array
- std::size_t
- std::nullptr_t
- std::is_integral
- std::rank
- std::is_void
- std::is_null_pointer
- std::is_array
- std::is_pointer
- std::is_enum
- std::is_union
- std::is_class
- std::is_function
- std::is_object
- std::is_scalar
- std::is_compound
- std::is_floating_point
- std::is_fundamental
- std::is_arithmetic
- std::is_reference
- std::is_lvalue_reference
- std::is_rvalue_reference
- std::is_member_pointer
- std::is_member_object_pointer
- std::is_member_function_pointer
- std::is_const
- std::is_volatile
- std::is_empty
- std::is_polymorphic
- std::is_final
- std::is_abstract
- std::is_trivial
- std::is_trivially_copyable
- std::is_standard_layout
- std::is_literal_type
- std::is_pod
- std::is_signed
- std::is_unsigned
- std::is_constructible, std::is_trivially_constructible, std::is_nothrow_constructible
- std::is_default_constructible, std::is_trivially_default_constructible, std::is_nothrow_default_constructible
- std::is_copy_constructible, std::is_trivially_copy_constructible, std::is_nothrow_copy_constructible
- std::is_move_constructible, std::is_trivially_move_constructible, std::is_nothrow_move_constructible
- std::is_assignable, std::is_trivially_assignable, std::is_nothrow_assignable
- std::is_copy_assignable, std::is_trivially_copy_assignable, std::is_nothrow_copy_assignable
- std::is_move_assignable, std::is_trivially_move_assignable, std::is_nothrow_move_assignable
- std::is_destructible, std::is_trivially_destructible, std::is_nothrow_destructible
- std::has_virtual_destructor
- std::is_same
- std::is_base_of
- std::is_convertible, std::is_nothrow_convertible
- std::is_layout_compatible
- std::is_pointer_interconvertible_base_of
- std::is_pointer_interconvertible_with_class
- std::is_corresponding_member
- std::alignment_of
- std::extent
- std::remove_cv, std::remove_const, std::remove_volatile
- std::add_cv, std::add_const, std::add_volatile
- std::make_signed
- std::make_unsigned
- std::remove_reference
- std::add_lvalue_reference, std::add_rvalue_reference
- std::remove_pointer
- std::add_pointer
- std::remove_extent
- std::remove_all_extents
- std::aligned_storage
- std::aligned_union
- std::decay
- std::enable_if
- std::void_t
- std::conditional
- std::common_type
- std::common_reference
- std::underlying_type
- std::type_identity
- 注é
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼ > std::is_constructible, std::is_trivially_constructible, std::is_nothrow_constructible
std::is_constructible, std::is_trivially_constructible, std::is_nothrow_constructible
| å®ä¹äºå¤´æä»¶ <type_traits>
|
||
| template< class T, class... Args > struct is_constructible; |
(1) | (C++11 èµ·) |
| template< class T, class... Args > struct is_trivially_constructible; |
(2) | (C++11 èµ·) |
| template< class T, class... Args > struct is_nothrow_constructible; |
(3) | (C++11 èµ·) |
1) è¥ T æ¯å¯¹è±¡æå¼ç¨ç±»åï¼ä¸åéå®ä¹ T obj(std::declval<Args>()...); ä¸ºè¯æï¼åæä¾çäº true çæå常é value ãææå
¶ä»æ
åµä¸ï¼ value 为 false ã
ä¸ºæ¤æ£æ¥çç®çï¼å³ä¸è½¬è¯åéå®ä¹ä¸ºå½æ°å£°æï¼ä¸ä¸è®¤ä¸º std::declval æ¯ odr 使ç¨ãå¦åä»ä¸ T åä»»ä½ Args ä¸ç±»åæ å
³çè¯å¢è¿è¡è®¿é®æ£æ¥ãä»
èèåéå®ä¹çç«å³è¯å¢çåæ³æ§ã
2) å 1) ï¼ä½åéå®ä¹ä¸è°ç¨ä»»ä½é平塿ä½ãä¸ºæ¤æ£æ¥çç®çï¼å¯¹ std::declval çè°ç¨è¢«è®¤ä¸ºæ¯å¹³å¡çã
3) å 1) ï¼ä½åéå®ä¹ä¸º noexcept ã
T ä¸åæ°å
Args ä¸çææç±»ååºåä¸ºå®æ´ç±»åãï¼å¯ä¸º cv éå®çï¼ void ï¼ææªç¥è¾¹çæ°ç»ãå¦åè¡ä¸ºæªå®ä¹ã
è¥ä¸è¿°æ¨¡æ¿çå®ä¾åç´æ¥æé´æ¥å°ä¾èµäºä¸å®æ´ç±»åï¼å¹¶ä¸å¦æåå¦ä½¿è¯¥ç±»å宿´ï¼å®ä¾åå°±ä¼äº§çä¸åçç»æï¼åè¡ä¸ºæªå®ä¹ã
æ·»å æ¤é¡µé¢ä¸æè¿°ç任使¨¡æ¿çç¹åçç¨åºè¡ä¸ºæªå®ä¹ã
è¾ å©å鿍¡æ¿
| template< class T, class... Args > inline constexpr bool is_constructible_v = is_constructible<T, Args...>::value; |
(C++17 èµ·) | |
| template< class T, class... Args > inline constexpr bool is_trivially_constructible_v = is_trivially_constructible<T, Args...>::value; |
(C++17 èµ·) | |
| template< class T, class... Args > inline constexpr bool is_nothrow_constructible_v = is_nothrow_constructible<T, Args...>::value; |
(C++17 èµ·) | |
ç»§æ¿èª std::integral_constant
æå常é
| value [éæ] |
è¥ T å¯ä» Args... æé å为 true ï¼å¦å为 false (å ¬å¼éææå常é) |
æå彿°
| operator bool |
转æ¢å¯¹è±¡ä¸º bool ï¼è¿å value (å ¬å¼æå彿°) |
| operator() (C++14) |
è¿å value (å ¬å¼æå彿°) |
æåç±»å
| Â | |
| ç±»å | å®ä¹ |
value_type
|
bool
|
type
|
std::integral_constant<bool, value> |
注æ
夿°å®ç°ä¸ï¼ is_nothrow_constructible äº¦æ£æ¥ææå½æ°æ¯å¦æåºï¼å 为å®çæäº noexcept(T(arg)) ãåæ ·çæ
åµåºç¨äº is_trivially_constructible ï¼å¨è¿äºå®ç°ä¸äº¦è¦æ±ææå½æ°ä¸ºå¹³å¡ï¼ GCC æ¼æ´ 51452 ã LWG é®é¢ 2116 ã
示ä¾
#include <iostream> #include <type_traits> Â class Foo { int v1; double v2; public: Foo(int n) : v1(n), v2() {} Foo(int n, double f) noexcept : v1(n), v2(f) {} }; Â int main() { std::cout << "Foo is ...\n" << std::boolalpha << "\tTrivially-constructible from const Foo&? " << std::is_trivially_constructible<Foo, const Foo&>::value << '\n' << "\tTrivially-constructible from int? " << std::is_trivially_constructible<Foo, int>::value << '\n' << "\tConstructible from int? " << std::is_constructible<Foo, int>::value << '\n' << "\tNothrow-constructible from int? " << std::is_nothrow_constructible<Foo, int>::value << '\n' << "\tNothrow-constructible from int and double? " << std::is_nothrow_constructible<Foo, int, double>::value << '\n'; }
è¾åºï¼
Foo is ...
Trivially-constructible from const Foo&? true
Trivially-constructible from int? false
Constructible from int? true
Nothrow-constructible from int? false
Nothrow-constructible from int and double? trueåé
| æ£æ¥ç±»åæ¯å¦æé»è®¤æé 彿° (类模æ¿) | |
| (C++11)(C++11)(C++11) |
æ£æ¥ç±»åæ¯å¦æ¥æå¤å¶æé 彿° (类模æ¿) |
| (C++11)(C++11)(C++11) |
æ£æ¥ç±»åæ¯å¦è½ä»å³å¼å¼ç¨æé (类模æ¿) |
| (C++11) |
æ£æ¥æå®çç±»åæ¯å¦æ¯æä½¿ç¨åé
å¨çæé (类模æ¿) |