在我无限探索可用作非类型模板参数的限制时,我试图查看是否可以std::source_location
用作非类型模板参数。这失败了一个奇怪的消息,因为我认为 source_location 是一些神奇的结构......
非类型模板参数的类型“std::experimental::source_location”不是结构类型
它失败了,所以我尝试使用 .file_name 来解决这个问题,但这也失败了(godbolt)。
注意:候选模板被忽略:替换失败:模板参数中不允许指向字符串字面量子对象的指针
#include<iostream>
#include<experimental/source_location>
template<auto src_loc = std::experimental::source_location::current().file_name()>
void log_first(){
static bool dummy =([]{
std::cout << "Logging first call" + src_loc << std::endl;
}(), false);
}
int main() {
log_first();
log_first();
}
有没有办法在不使用宏的情况下完成这项工作?
需要明确的是,我问的是source_location
用作模板参数,而不是解决我的玩具示例,它只是为了演示潜在的用例。