Updated:

less than 1 minute read

개요

  • 파일 이름, 소스 라인 번호, 함수 이름 등 소스 코드에 대한 특정 정보를 나타내는 클래스
  • 기존의 __LINE__, __FILE__ 등에 대한 더 나은 대안


예제

  • 코드
     #include <iostream>
     #include <source_location>
        
     using namespace std;
        
     int main() {
     	auto print = [](source_location sl = source_location::current()) {
     		cout << sl.line() << endl;
     		cout << sl.column() << endl;
     		cout << sl.file_name() << endl;
     		cout << sl.function_name() << endl;
     	};
     	print();
        
     	cout << "------" << endl;
        
     	auto func1 = [print]() { print(); };
     	func1();
        
     	cout << "------" << endl;
        
     	auto func2 = [print](int i) { print(); };
     	func2(1);
        
     	return 0;
     }
    
  • 실행 결과
     13
     7
     main.cpp
     int main()
     ------
     17
     32
     main.cpp
     main()::<lambda()>
     ------
     22
     37
     main.cpp
     main()::<lambda(int)>