C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- std::apply
- åºç¹æ§æµè¯å® (C++20)
- ç¨åºæ¯æå·¥å ·
- std::initializer_list
- 彿°å¯¹è±¡
- std::hash
- std::pair
- std::tuple
- std::optional
- std::any
- std::variant
- æ ¼å¼ååº (C++20)
- std::integer_sequence
- std::exchange
- std::make_from_tuple
- std::launder
- std::to_chars
- std::from_chars
- std::as_const
- std::source_location
- å忰彿°
- std::bitset
- std::cmp_equal, cmp_not_equal, cmp_less, cmp_greater, cmp_less_equal, cmp_greater_equal
- std::in_range
- std::declval
- std::forward
- std::move
- std::move_if_noexcept
- std::chars_format
- std::piecewise_construct_t
- std::piecewise_construct
- std::in_place, std::in_place_type, std::in_place_index, std::in_place_t, std::in_place_type_t, std::in_place_index_t
- 注é
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >å·¥å ·åº > std::source_location
std::source_location
| å®ä¹äºå¤´æä»¶ <source_location>
|
||
| struct source_location; |
(C++20 èµ·) | |
source_location 类表示å
³äºæºç çå
·ä½ä¿¡æ¯ï¼ä¾å¦æä»¶åãè¡å·ä»¥å彿°åã以åï¼å¸æè·å¾å
³äºè°ç¨ä½ç½®çä¿¡æ¯ï¼ç¨äºè®°å½ãæµè¯æè°è¯ç®çï¼ç彿°å¿
须使ç¨å®ï¼ä»¥ä»¤å¦ __LINE__ ä¸ __FILE__ çé¢å®ä¹å®äºè°ç¨æ¹çç¯å¢å±å¼ã source_location ç±»æä¾æ´å¥½çæ¿ä»£ã
source_Âlocation 符åå¯é»è®¤æé (DefaultConstructible) ãå¯å¤å¶æé (CopyConstructible) ãå¯å¤å¶èµå¼ (CopyAssignable) å坿æ (Destructible) è¦æ±ã source_location çå·¦å¼ç¬¦åå¯äº¤æ¢ (Swappable) è¦æ±ã
å¦å¤ï¼ä¸åæ¡ä»¶ä¸º true ï¼
- std::is_nothrow_move_constructible_v<std::source_location> ã
- std::is_nothrow_move_assignable_v<std::source_location> å
- std::is_nothrow_swappable_v<std::source_location> ã
ææä»¤ source_location æ¥æè¾å°ç大å°å¹¶ä¸è½é«æå¤å¶ã
source_location çå¤å¶/ç§»å¨æé 彿°ä¸å¤å¶/ç§»å¨èµå¼è¿ç®ç¬¦æ¯å¦ä¸ºå¹³å¡å/æ constexpr æ¯æªææçã
æå彿°
å建 | |
æé æ¥æå®ç°å®ä¹çå¼çæ° source_location (å ¬å¼æå彿°) | |
| [éæ] |
æé 对åºè°ç¨ç¹ä½ç½®çæ° source_location (å ¬å¼éææå彿°) |
åè®¿é® | |
| è¿åæ¤å¯¹è±¡æè¡¨ç¤ºçè¡å· (å ¬å¼æå彿°) | |
| è¿åæ¤å¯¹è±¡æè¡¨ç¤ºçåå· (å ¬å¼æå彿°) | |
| è¿åæ¤å¯¹è±¡æè¡¨ç¤ºçæä»¶å (å ¬å¼æå彿°) | |
| è¿åæ¤å¯¹è±¡è¡¨ç¤ºç彿°åï¼è¥å®åå¨ (å ¬å¼æå彿°) | |
示ä¾
#include <iostream> #include <string_view> #include <source_location> Â void log(std::string_view message, const std::source_location& location = std::source_location::current()) { std::cout << "info:" << location.file_name() << ":" << location.line() << " " << message << '\n'; } Â int main() { log("Hello world!"); }
å¯è½çè¾åºï¼
info:main.cpp:15 Hello world!