std::source_location
cppreference.com
<tbody>
</tbody>
| <source_location> 에 정의되어 있음.
|
||
struct source_location; |
(since C++20) | |
source_location 클래스는 파일 이름이나 줄 번호, 함수 이름과 같은 소스코드에 대한 확정적인 정보를 표현합니다. 이전에는 로깅이나 테스트, 디버그 목적으로 이 정보를 확득하려는 함수는 호출자의 컨텍스트에서 확장되는 __LINE__ and __FILE__ 와 같은 미리 정의된 매크로를 사용해야 했습니다. source_location 클래스는 더 나은 대안을 제공합니다.
source_location 는 DefaultConstructible, CopyConstructible, CopyAssignable 와 Destructible 를 충족합니다. source_location 의 Lvalue는 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 의 copy/move 생성자와 copy/move 할당 연산자가 컴파일러 기본 타입인지와 constexpr 인지의 여부는 명시되지 않았습니다.
Member functions
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) | |
Example
코드 실행
#include <iostream>
#include <string_view>
#include <source_location>
void log(const std::string_view message,
const std::source_location location =
std::source_location::current())
{
std::cout << "file: "
<< location.file_name() << "("
<< location.line() << ":"
<< location.column() << ") `"
<< location.function_name() << "`: "
<< message << '\n';
}
template <typename T> void fun(T x)
{
log(x);
}
int main(int, char*[])
{
log("Hello world!");
fun("Hello C++20!");
}
Possible output:
file: main.cpp(23:8) `int main(int, char**)`: Hello world!
file: main.cpp(18:8) `void fun(T) [with T = const char*]`: Hello C++20!
See also
(C++23) |
스택추적 내용을 나타냅니다 (class) |
| Filename and line information | |