std:: source_location
|
定義先ヘッダ
<source_location>
|
||
|
struct
source_location
;
|
(C++20以降) | |
std::source_location
クラスは、ファイル名、行番号、関数名など、ソースコードに関する特定の情報を表します。以前は、呼び出し元の情報(ロギング、テスト、デバッグ目的など)を取得したい関数は、
定義済みマクロ
である
__LINE__
や
__FILE__
が呼び出し元のコンテキストで展開されるように、マクロを使用する必要がありました。
std::source_location
クラスはこれより優れた代替手段を提供します。
std::source_location
は
DefaultConstructible
、
CopyConstructible
、
CopyAssignable
、
Destructible
および
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 > 。
std::source_location
は、サイズが小さく効率的にコピーできることを意図しています。
std::source_location
のコピー/ムーブコンストラクタおよびコピー/ムーブ代入演算子がトリビアルであるかどうか、およびconstexprであるかどうかは未規定です。
目次 |
メンバー関数
Creation |
|
実装定義の値で新しい
source_location
を構築する
(public member function) |
|
|
[static]
|
呼び出し元の位置に対応する新しい
source_location
を構築する
(public static member function) |
Field access |
|
|
このオブジェクトが表す行番号を返す
(public member function) |
|
|
このオブジェクトが表す列番号を返す
(public member function) |
|
|
このオブジェクトが表すファイル名を返す
(public member function) |
|
|
このオブジェクトが表す関数名を返す(存在する場合)
(public member function) |
|
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_source_location
|
201907L
|
(C++20) |
ソースコード情報取得 (
std::source_location
)
|
例
#include <iostream> #include <source_location> #include <string_view> void log(const std::string_view message, const std::source_location location = std::source_location::current()) { std::clog << "file: " << location.file_name() << '(' << location.line() << ':' << location.column() << ") `" << location.function_name() << "`: " << message << '\n'; } template<typename T> void fun(T x) { log(x); // line 20 } int main(int, char*[]) { log("Hello world!"); // line 25 fun("Hello C++20!"); }
出力例:
file: main.cpp(25:8) `int main(int, char**)`: Hello world! file: main.cpp(20:8) `void fun(T) [with T = const char*]`: Hello C++20!
関連項目
|
ソースコードの行番号と、オプションで現在のファイル名を変更する
(前処理ディレクティブ) |
|
|
(C++23)
|
スタックトレース内の評価の表現
(クラス) |