库基础知识的C++扩展,版本2(N4564)介绍了该类型std::experimental::source_location.
§14.1.2[reflection.src_loc.creation]说:
Run Code Online (Sandbox Code Playgroud)static constexpr source_location current() noexcept;返回:当函数调用(C++14§5.2.2)调用其后缀表达式(可能是带括号的)id-expression命名时
current,返回source_location带有实现定义值的a.该值应受#line(C++14§16.4)影响,其方式与__LINE__和__FILE__.如果以其他方式调用,则返回的值未指定.备注:当使用大括号或等于初始化程序来初始化非静态数据成员时,任何调用都
current应该对应于构造函数的位置或初始化成员的聚合初始化.[ 注意:当用作默认参数(C++14§8.3.6)时,该值
source_location将是current呼叫站点呼叫的位置.- 结束说明 ]
如果我理解正确,那么该功能就像这样使用.
#include <experimental/source_location> // I don't actually have this header
#include <iostream>
#include <string>
#include <utility>
struct my_exception
{
std::string message {};
std::experimental::source_location location {};
my_exception(std::string msg,
std::experimental::source_location loc = std::experimental::source_location::current()) :
message {std::move(msg)},
location {std::move(loc)}
{ …Run Code Online (Sandbox Code Playgroud)