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_constant_evaluated
std::is_constant_evaluated
| å®ä¹äºå¤´æä»¶ <type_traits>
|
||
| constexpr bool is_constant_evaluated() noexcept; |
(C++20 èµ·) | |
æ£æ¥å½æ°è°ç¨æ¯å¦åºç°å¨å¸¸éæ±å¼çåºåãè¥å¯¹è°ç¨çæ±å¼åºç°å¨ææ¾å¸¸éæ±å¼çè¡¨è¾¾å¼æç±»å转æ¢çæ±å¼ä¸ï¼è¿å true ï¼å¦åè¿å false ã
以ä¸è¡¨è¾¾å¼ï¼å æ¬å°ç®æ ç±»åçéå¼ç±»å转æ¢ï¼æ¯ææ¾å¸¸éæ±å¼çï¼
- è¯æ³ä¸è¦æ±å¸¸é表达å¼çåºåï¼å æ¬
- æ°ç»é¿åº¦
- new 表达å¼ä¸é¤ç¬¬ä¸ç»´ä»¥å¤çæ°ç»é¿åº¦
- ä½åé¿åº¦
- æä¸¾é¡¹åå§åå¨
- 对é½
- case 表达å¼
- éç±»åæ¨¡æ¿å®å
- noexcept è§å®
-
static_assert声æ - æ¡ä»¶æ§
explicitæå®ç¬¦
- constexpr if è¯å¥çæ¡ä»¶
- ç«å³è°ç¨
- æ¦å¿µå®ä¹ãåµå¥è¦æ±å requires åå¥ä¸ç表达å¼
- å¯ç¨äºå¸¸é表达å¼çåéçåå§åå¨ï¼å æ¬
- constexpr åéçåå§åå¨
- å¼ç¨å带 const éå®çæ´æ°ææä¸¾ç±»åçåéçåå§åå¨ï¼è¥è¯¥åå§åå¨ä¸ºå¸¸é表达å¼
- éæå线ç¨å±ååéçåå§åå¨ï¼è¥è¯¥åå§åå¨çææå表达å¼ï¼å«æé 彿°è°ç¨åéå¼è½¬æ¢ï¼é½æ¯å¸¸é表达å¼ï¼å³è¯¥åå§åå¨ä¸ºå¸¸éåå§åå¨ï¼
æµè¯æå两个æ¡ä»¶å¯è½æ¶å对åå§åå¨çè¯æ¢æ§å¸¸éæ±å¼ãä¸å»ºè®®ä¾èµæ¤æ¶çç»æã
int y; const int a = std::is_constant_evaluated() ? y : 1; // è¯æ¢æ§å¸¸éæ±å¼å¤±è´¥ï¼å¸¸éæ±å¼è¢«èå¼ã // åé a 卿åå§å为 1  const int b = std::is_constant_evaluated() ? 2 : y; // 叏鿱å¼ï¼std::is_constant_evaluation() == trueï¼æåã // åé b éæåå§å为 2
åæ°
ï¼æ ï¼
è¿åå¼
è¥å¯¹è°ç¨çæ±å¼åºç°å¨ææ¾å¸¸éæ±å¼çè¡¨è¾¾å¼æç±»å转æ¢çæ±å¼ä¸ï¼è¿å true ï¼å¦åè¿å false ã
注æ
std::is_constant_evaluated() ç´æ¥ä½ä¸º static_assert 声æå constexpr if è¯å¥çæ¡ä»¶æ¶ï¼è¿å弿»æ¯ true ã
示ä¾
#include <type_traits> #include <cmath> #include <iostream>  constexpr double power(double b, int x) { if (std::is_constant_evaluated() && !(b == 0.0 && x < 0)) { // 叏鿱å¼è¯å¢ï¼ä½¿ç¨ constexpr å好çç®æ³ã if (x == 0) return 1.0; double r = 1.0, p = x > 0 ? b : 1.0 / b; auto u = unsigned(x > 0 ? x : -x); while (u != 0) { if (u & 1) r *= p; u /= 2; p *= p; } return r; } else { // 令代ç çæå¨çæã return std::pow(b, double(x)); } }  int main() { // 常é表达å¼è¯å¥ constexpr double kilo = power(10.0, 3); int n = 3; // é常é表达å¼ï¼å 为 n ä¸è½å¨å¸¸é表达å¼è¯å¢ä¸è½¬æ¢æå³å¼ // çä»·äº std::pow(10.0, double(n)) double mucho = power(10.0, n);  std::cout << kilo << " " << mucho << "\n"; // (3) }
è¾åºï¼
1000 1000