c++ - 模板类型名

标签 c++ templates types typename

C++ 标准 是否以某种方式指定了 T 在以下声明中可以是什么:

template <typename T>

我的意思是,从实用的角度来看,这可以是任何特定类型,它允许模板编译(当相应的替换发生时)

但是严格定义呢?

最佳答案

如你所愿,这里是标准:

C++03, 14.1, 模板参数:

A template defines a family of classes or functions.

template-declaration:
    exportopt template < template-parameter-list > declaration
template-parameter-list:
    template-parameter
     template-parameter-list , template-parameter

template-parameter:
    type-parameter
    parameter-declaration
type-parameter:
    class identifieropt
    class identifieropt = type-id
    typename identifieropt
    typename identifieropt = type-id
    template < template-parameter-list > class identifieropt
    template < template-parameter-list > class identifieropt = id-expression

..

A type-parameter defines its identifier to be a type-name (if declared with class or typename) or template-name (if declared with template) in the scope of the template declaration.

..

If the use of a template-argument gives rise to an ill-formed construct in the instantiation of a template specialization, the program is ill-formed.

其他内容用于默认参数、非类型模板等。换句话说,标准没有说明任何关于 T 的内容。

关于c++ - 模板类型名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8204608/

相关文章:

c++ - 你如何确定 MySQLCPPConn 库文件在哪里?

c++ - p 的正 n 次方根

c++ - 行为处理列表和标量实体的通用打印函数?

c++ - 为什么 std::is_invocable 不接受非类型模板参数

class - 我如何在 Scala 中引用未实例化的类?

haskell - 为什么这个函数签名不进行类型检查?

c++ - 使用Qt模拟gps数据以与traccar一起使用

c# - 如何跳过C#程序中的C++ dll错误

powershell - 如何在PowerShell中将字符串值转换为动态数据类型?

c++ - 是否可以在类定义之外以某种方式提供相当于 operator bool cast 的东西?