std :: source_location作为非类型模板参数 [英] std::source_location as non type template parameter

查看:65
本文介绍了std :: source_location作为非类型模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在无穷无尽的尝试中,尝试限制可以用作非类型模板参数的值,我试图查看是否可以将 std :: source_location 用作非类型模板参数.由于我认为source_location是一些神奇的结构,因此失败并发出一条奇怪的消息.

In my infinite quest to push limits of what can be used as non type template parameter I was trying to see if I can use std::source_location as non type template parameter. That failed with a weird message, since I presume source_location is some magical struct...

非类型模板的

类型'std :: experimental :: source_location'参数不是结构类型

type 'std::experimental::source_location' of non-type template parameter is not a structural type

它失败了,所以我尝试使用.file_name来解决,但这也失败了( godbolt ).

It failed, so I tried to workaround that with using .file_name, but that also fails (godbolt).

注:候选模板被忽略:替换失败:指向的指针模板参数中不允许使用字符串文字的子对象

note: candidate template ignored: substitution failure: pointer to subobject of string literal is not allowed in a template argument

#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();
}

有什么方法可以在不使用宏的情况下完成这项工作?

Is there any way to make this work without use of macros?

要清楚,我要问的是使用 source_location 作为模板参数,而不是解决我的玩具示例,这只是在演示潜在的用例.

To be clear I am asking about using source_location as template parameter, not about solving my toy example, it is just here to demonstrate potential use case.

推荐答案

并且可用作非模板模板参数的类型类型的规则要求类型为 structural ,这意味着从

And the rules for the kinds of types that can be used as non-template template parameters require that a type be structural, which means, from [temp.param]/7, emphasis mine:

结构类型是以下之一:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆