Namespaces
Variants

std::source_location:: function_name

From cppreference.net
Utilities library
constexpr const char * function_name ( ) const noexcept ;
(C++20부터)

이 객체가 나타내는 위치와 연관된 함수의 이름을 반환합니다(있는 경우).

목차

매개변수

(없음)

반환값

이 객체가 함수 본문 내의 위치를 나타내는 경우, 함수 이름에 해당하는 구현 정의(implementation-defined) 널 종료 바이트 문자열을 반환합니다.

그렇지 않으면 빈 문자열이 반환됩니다.

예제

std::source_location::function_name 는 함수 이름(특수 함수 포함)과 시그니처를 함께 얻는 데 도움을 줄 수 있습니다.

#include <cstdio>
#include <utility>
#include <source_location>
inline void print_function_name(
    const std::source_location& location = std::source_location::current())
{
    std::puts(location.function_name()); // prints the name of the caller
}
void foo(double &&) { print_function_name(); }
namespace bar { void baz() { print_function_name(); } }
template <typename T> auto pub(T) { print_function_name(); return 42; }
struct S {
    S() { print_function_name(); }
    S(int) { print_function_name(); }
    ~S() { print_function_name(); }
    S& operator=(S const&) { print_function_name(); return *this; }
    S& operator=(S&&) { print_function_name(); return *this; }
};
int main(int, char const* const[])
{
    print_function_name();
    foo(3.14);
    bar::baz();
    pub(0xFULL);
    S p;
    S q{42};
    p = q;
    p = std::move(q);
    [] { print_function_name(); }();
}

가능한 출력:

int main(int, const char* const*)
void foo(double&&)
void bar::baz()
auto pub(T) [with T = long long unsigned int]
S::S()
S::S(int)
S& S::operator=(const S&)
S& S::operator=(S&&)
main(int, const char* const*)::<lambda()>
S::~S()
S::~S()

참고 항목

이 객체가 나타내는 줄 번호를 반환합니다
(public member function)
이 객체가 나타내는 열 번호를 반환합니다
(public member function)
이 객체가 나타내는 파일 이름을 반환합니다
(public member function)