Template typename
Template typename
C++ standard
是否在以下声明中以某种方式指定了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
..
类型形参将其标识符定义为模板声明范围内的类型名称(如果用class或typename声明)或模板名称(如果用template声明)。
.
如果在模板专门化的实例化中使用模板实参导致了不良形式的构造,则该程序是不良形式的。
其他部分用于默认参数、非类型模板等。换句话说,该标准没有关于T
的任何内容。
程序员有责任确保用于T
的数据类型是兼容的,并且具有将在T
上执行的所有必要操作。就c++标准而言,任何数据类型都可以用来代替T
。
没有严格的定义,因为这似乎违背了模板的目的。T
是任何类型,例如,作为实参传递给具有T
类型形参的函数。
为了模板的代码可重用性,你牺牲了严格类型定义的安全性。有了这种自由,您需要提供检查,以确保T
是该函数的合理类型。
boost有一个有用的模板enable_if,它允许您仅为特定类型启用模板。
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 传递给std::function template的template参数究竟代表什么
- 在template中使用std::variant的template函数
- 为什么返回类型中需要typename?C++
- C++ template for QList
- C++ - 为什么这里需要'template'关键字?
- C++ assigment std::list:<typename>:itrator 在 main 中工作,但在方法中它不起作用
- 使用 'typename' 关键字将非类型视为依赖上下文中的类型
- 使用"std::enable_if_t" "function template has already been defined"
- 在"template"和函数声明之间使用:template<typename trait> using tr = base_trait<trait> void fn(tr::t
- C++ 中的"template <typename From, typename Tag> struct Alias;"是什么?
- 为什么可变参数模板在模板介绍中不起作用,但在要求子句中起作用?ConceptName{T,U,V,W} <-- template<typename ...T>
- "typename"和"template"关键词:它们真的有必要吗?
- 在不需要时使用"template"和"typename"消除歧义器
- 等同于"typename",表示从属姓名确实是'template template parameter'
- Template typename
- C++:模板成员函数,如 template<typename T> int foo<T>()
- C++ typedef typename classname::template
- 再次强调typename和template关键字
- template<typename A B>