Namespaces
Variants

std:: source_location

From cppreference.net
Utilities library
헤더 파일에 정의됨 <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::source_location 는 작은 크기를 가지며 효율적으로 복사될 수 있도록 의도되었습니다.

std::source_location 의 복사/이동 생성자와 복사/이동 할당 연산자가 trivial하고/또는 constexpr인지 여부는 명시되어 있지 않습니다.

목차

멤버 함수

생성
구현 정의 값으로 새로운 source_location 을 생성함
(public member function)
[static]
호출 위치에 해당하는 새로운 source_location 을 생성함
(public static member function)
필드 접근
이 객체가 나타내는 줄 번호를 반환함
(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!

참고 항목

소스 코드의 줄 번호와 선택적으로 현재 파일 이름을 변경합니다
(전처리기 지시문)
스택 트레이스에서 평가의 표현
(클래스)