c++ - 为什么叫 "non-type"模板参数?

标签 c++ templates non-type

在 C++ 模板术语中,我们有非类型模板参数、类型模板参数和模板模板参数(然后是带参数的相同列表)。

为什么叫非类型?它不是一个吗?不应该是“值模板参数”吗?

如果我将它们视为值模板参数,我会错过什么吗?

注意:出于好奇,我查看了 D 语言的文档,他们称之为 value。

最佳答案

“值”在 C++ 中有一个非常具体的非直观定义,不一定适用于非类型模板参数:

3.9 Types [basic.types]

4 The object representation of an object of type T is the sequence of N unsigned char objects taken up by the object of type T, where N equals sizeof(T). The value representation of an object is the set of bits that hold the value of type T. For trivially copyable types, the value representation is a set of bits in the object representation that determines a value, which is one discrete element of an implementation-defined set of values.

尽管 C++ 标准偶尔会非正式地使用“值”这个词,但他们在这里没有这样做是件好事。非类型模板参数类型不需要是可简单复制的。具体来说,正如 Mike Seymour 对这个问题的评论,它可能是一个引用类型。

也就是说,我确实觉得我应该注意我认为术语“非类型模板参数”是不正确的。以前是这样,但现在我们有了模板模板参数,它们不是类型,也不是非类型模板参数。

关于c++ - 为什么叫 "non-type"模板参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26861834/

相关文章:

c++ - 将模板化类传递给模板化函数

c++ - 为什么模板非类型参数指针和引用参数需要是全局的

java - OpenCV Mat::Mul 的参数

不使用指针或数组的 C++ 损坏堆

c++ - 为什么函数模板不能访问前向声明的类型?

c++ - 具有 const 参数的模板未按预期分派(dispatch)

c++ - std::vector 的模板化迭代器

c++ - C++17和C++11中的非类型模板参数有什么区别?

c++ - 朱利叶斯警告 : strip: sample 0-255 is invalid, 已剥离

c++ - 挥之不去的打开文件导致 "Too many open files"