5go*_*der 8 c++ reflection fundamentals-ts c++17 std-source-location
库基础知识的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)}
{
}
};
int
do_stuff(const int a, const int b)
{
if (a > b)
throw my_exception {"a > b"}; // line 25 of file main.cxx
return b - a;
}
int
main()
{
try
{
std::cout << do_stuff(2, 1) << "\n";
}
catch (const my_exception& e)
{
std::cerr << e.location.file_name() << ":" << e.location.line() << ": "
<< "error: " << e.message << "\n";
}
}
Run Code Online (Sandbox Code Playgroud)
预期产量:
main.cxx:25: error: a > b
Run Code Online (Sandbox Code Playgroud)
如果没有std::experimental::source_location,我们可能已经使用了一个辅助宏THROW_WITH_SOURCE_LOCATION,它在内部使用__FILE__和__LINE__宏来正确初始化异常对象.
我想知道图书馆如何实施std::experimental::source_location.除非我完全忽略了这一点,否则如果没有特殊的编译器支持,这样做是不可能的.但是需要什么样的魔术编译器功能才能使其工作?它可以与部署的技巧相媲美std::initializer_list吗?是否有可用于查看此功能的实验性实现?我检查了GCC的SVN来源,但还没有找到任何东西.
Pra*_*ian 10
实现这一点需要编译器的支持.例如,用gcc,你可能使用内置插件功能类似
Run Code Online (Sandbox Code Playgroud)int __builtin_LINE()此函数等效于预处理器
__LINE__宏,并返回内置调用的行号.在函数的C++默认参数中F,它获取调用的行号F.Run Code Online (Sandbox Code Playgroud)const char * __builtin_FUNCTION()此函数等效于预处理器
__FUNCTION__宏,并返回内置调用所在的函数名称.Run Code Online (Sandbox Code Playgroud)const char * __builtin_FILE()此函数等效于预处理器
__FILE__宏,并返回内置调用所在的文件名.在函数的C++默认参数中F,它获取调用的文件名F.
| 归档时间: |
|
| 查看次数: |
2145 次 |
| 最近记录: |