相关疑难解决方法(0)

如何实现std :: experimental :: source_location?

库基础知识的C++扩展,版本2(N4564)介绍了该类型std::experimental::source_location.

§14.1.2[reflection.src_loc.creation]说:

static constexpr source_location current() noexcept;
Run Code Online (Sandbox Code Playgroud)

返回:当函数调用(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)

c++ reflection fundamentals-ts c++17 std-source-location

8
推荐指数
1
解决办法
2145
查看次数