Namespaces
Variants

std::source_location:: source_location

From cppreference.net
Utilities library
constexpr source_location ( ) noexcept ;
(1) (seit C++20)
source_location ( const source_location & other ) ;
(2) (seit C++20)
source_location ( source_location && other ) noexcept ;
(3) (seit C++20)
1) Konstruiert ein source_location -Objekt mit nicht spezifiziertem Wert.
2,3) Copy- und Move-Konstruktoren. Es ist nicht spezifiziert, ob sie trivial und/oder constexpr sind.

Parameter

other - ein weiteres source_location zum Kopieren oder Verschieben

Beispiel

#include <iomanip>
#include <iostream>
#include <ranges>
#include <source_location>
#include <string_view>
#include <vector>
// GCC-spezifischer Typname-Drucker
#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"
              << "  Dateiname     : " << std::quoted(l.file_name()) << '\n'
              << "  Funktionsname : " << std::quoted(l.function_name()) << '\n'
              << "  Zeile         : " << l.line() << '\n'
              << "  Spalte        : " << l.column() << '\n';
}
int main()
{
    constexpr std::source_location default_constructed;
    print("Standardkonstruiert", default_constructed);
    constexpr std::source_location current = std::source_location::current();
    print("Aktuell", 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
}
**Übersetzte Elemente:** - Kommentar: "// GCC specific type name printer" → "// GCC-spezifischer Typname-Drucker" - String-Literale in der `print`-Funktion: - "file_name" → "Dateiname" - "function_name" → "Funktionsname" - "line" → "Zeile" - "column" → "Spalte" - String-Literale in `main`: - "default constructed" → "Standardkonstruiert" - "current" → "Aktuell" **Nicht übersetzte Elemente:** - Alle HTML-Tags und Attribute - C++ Code innerhalb der `
` Tags
- C++ spezifische Begriffe (template, auto, constexpr, etc.)
- Technische Bezeichner und Funktionsnamen

Mögliche Ausgabe:

Standardkonstruiert:
  Dateiname     : ""
  Funktionsname : ""
  Zeile         : 0
  Spalte        : 0
Aktuell:
  Dateiname     : "main.cpp"
  Funktionsname : "int main()"
  Zeile         : 39
  Spalte        : 75
[int]
[double*]
[main()::<lambda()>]
[std::basic_string_view<char>]
[std::ranges::join_view<std::ranges::ref_view<const std::vector<std::vector<int> > > >]

Siehe auch

[static]
erzeugt ein neues source_location entsprechend dem Ort der Aufrufstelle
(öffentliche statische Elementfunktion)
erzeugt ein neues stacktrace_entry
(öffentliche Elementfunktion von std::stacktrace_entry )