c++ - 自动模板参数可以传什么有限制吗?

标签 c++ templates parameters c++17 auto

<分区>

我们得到了auto template parameters .我试图在这个问题中使用一个来传递一个对象:Can I Write Relational Operators in Terms of Arithmetic Operations?但导演AndyG's comment我发现没有编译:(

给定模板函数:

template <auto T>
void foo()

似乎对我可以作为模板参数传递的内容有限制。例如,如我的链接问题所示,我似乎无法传递仿函数:

foo<plus<int>{}>()

在某处是否有允许和不允许的列表?

最佳答案

我相信这完全由以下标准语句处理:

[temp.arg.nontype]
1: If the type T of a template-parameter ([temp.param]) contains a placeholder type ([dcl.spec.auto]) or a placeholder for a deduced class type ([dcl.type.class.deduct]), the type of the parameter is the type deduced for the variable x in the invented declaration

T x = template-argument ;

If a deduced parameter type is not permitted for a template-parameter declaration ([temp.param]), the program is ill-formed.

传递仿函数类型是允许的。传递仿函数实例不是,就像传递 struct A {}; 的实例一样不是。

关于允许使用哪些非类型模板参数:

4: A non-type template-parameter shall have one of the following (optionally cv-qualified) types:

(4.1) a type that is literal, has strong structural equality ([class.compare.default]), has no mutable or volatile subobjects, and in which if there is a defaulted member operator<=>, then it is declared public,

(4.2) an lvalue reference type,

(4.3) a type that contains a placeholder type ([dcl.spec.auto]), or

(4.4) a placeholder for a deduced class type ([dcl.type.class.deduct]).

5: [ Note: Other types are disallowed either explicitly below or implicitly by the rules governing the form of template-arguments ([temp.arg]). — end note  ] The top-level cv-qualifiers on the template-parameter are ignored when determining its type.

关于c++ - 自动模板参数可以传什么有限制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53761899/

相关文章:

c++:如何从UTF-8代码点创建unsigned char

php - 如何将参数传递给使用 'include' 呈现的 PHP 模板?

c++ - 将 float 转换为字符串

c++ - 二进制搜索树类 - 标识符 "ItemType"未定义

c++ - 获得向后插入器并回退到任何插入器的惯用方法是什么?

perl - 在 Catalyst 中显式调用 View

python - 在 Sphinx 中记录带有长描述的参数

javascript - 我的 if 语句给出了错误的输出

c++ - 在 C++ 中关闭 setf?

c++ - 我可以跨 DLL 边界传递 FILE 对象吗?