Namespaces
Variants

std::source_location:: source_location

From cppreference.net
Utilities library
constexpr source_location ( ) noexcept ;
(1) (C++20以降)
source_location ( const source_location & other ) ;
(2) (C++20以降)
source_location ( source_location && other ) noexcept ;
(3) (C++20以降)
1) 未指定の値を持つ source_location オブジェクトを構築します。
2,3) コピーおよびムーブコンストラクタ。これらが trivial および/または constexpr であるかどうかは未規定です。

パラメータ

その他 - コピーまたは移動元の別の source_location

#include <iomanip>
#include <iostream>
#include <ranges>
#include <source_location>
#include <string_view>
#include <vector>
// GCC固有の型名プリンター
#if (__GNUG__ >= 11)
template<typename T>
auto type_name_helper(const std::source_location s = std::source_location::current())
{
    using std::operator""sv;
    const std::string_view fun_name{s.function_name()};
    constexpr auto prefix{"[with T = "sv};
    const auto type_name_begin{fun_name.find(prefix)};
    if (""sv.npos == type_name_begin)
        return ""sv;
    const std::size_t first{type_name_begin + prefix.length()};
    return std::string_view{fun_name.cbegin() + first, fun_name.cend() - 1};
}
template<typename T>
auto type_name() { return type_name_helper<T>(); }
#endif
void print(std::string_view const comment, std::source_location const l)
{
    std::cout << comment << ":\n"
              << "  ファイル名     : " << std::quoted(l.file_name()) << '\n'
              << "  関数名 : " << std::quoted(l.function_name()) << '\n'
              << "  行番号          : " << l.line() << '\n'
              << "  列番号        : " << l.column() << '\n';
}
int main()
{
    constexpr std::source_location default_constructed;
    print("デフォルト構築", default_constructed);
    constexpr std::source_location current = std::source_location::current();
    print("現在位置", current);
#if (__GNUG__ >= 11)
    const std::vector<std::vector<int>> v{{1,2}, {3,4,5}, {6}};
    auto jv = std::ranges::join_view(v);
    std::cout << '\n'
              << '[' << type_name<int>() << "]\n"
              << '[' << type_name<double*>() << "]\n"
              << '[' << type_name<decltype([](){})>() << "]\n"
              << '[' << type_name<decltype(type_name<int>())>() << "]\n"
              << '[' << type_name<decltype(jv)>() << "]\n";
#endif
}

出力例:

デフォルト構築時:
  ファイル名     : ""
  関数名 : ""
  行番号          : 0
  カラム        : 0
現在:
  ファイル名     : "main.cpp"
  関数名 : "int main()"
  行番号          : 39
  カラム        : 75
[int]
[double*]
[main()::<lambda()>]
[std::basic_string_view<char>]
[std::ranges::join_view<std::ranges::ref_view<const std::vector<std::vector<int> > > >]

関連項目

[static]
呼び出し元の位置に対応する新しい source_location を構築する
(public static member function)
新しい stacktrace_entry を構築する
(public member function of std::stacktrace_entry )