std::experimental::source_location
可能会在某个时候添加到 C++ 标准中。我想知道是否可以将位置信息放入编译时领域。本质上,我想要一个从不同源位置调用时返回不同类型的函数。像这样的东西,虽然它没有编译,因为location
对象不是constexpr
函数参数:
#include <experimental/source_location>
using namespace std::experimental;
constexpr auto line (const source_location& location = source_location::current())
{
return std::integral_constant<int, location.line()>{};
}
int main()
{
constexpr auto ll = line();
std::cout << ll.value << '\n';
}
这不会编译,并带有关于的消息
expansion of [...] is not a constant expression
关于return std::integral_constant<int, location.line()>{}
线。如果我不能使用它们,那么拥有这些方法source_location
有什么好处?constexpr