std::source_location::source_location
来自cppreference.com
<tbody>
</tbody>
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) 复制与移动构造函数。它们是否为平凡和/或 constexpr 是未指定的。
参数
| other | - | 用以复制或移动的另一 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"
<< " file_name : " << std::quoted(l.file_name()) << '\n'
<< " function_name : " << std::quoted(l.function_name()) << '\n'
<< " line : " << l.line() << '\n'
<< " column : " << 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
}
可能的输出:
默认构造的:
file_name : ""
function_name : ""
line : 0
column : 0
当前的:
file_name : "main.cpp"
function_name : "int main()"
line : 39
column : 75
[int]
[double*]
[main()::<lambda()>]
[std::basic_string_view<char>]
[std::ranges::join_view<std::ranges::ref_view<const std::vector<std::vector<int> > > >]
参阅
[静态] |
构造对应调用点位置的新 source_location (公开静态成员函数) |
构造新的 stacktrace_entry ( std::stacktrace_entry 的公开成员函数)
|