`

template <unsigned int N>

    博客分类:
  • c++
阅读更多

详见:http://stackoverflow.com/questions/499106/what-does-template-unsigned-int-n-mean

 

You can have several kinds of template parameters

 

Type Parameters.

Types

Templates (only classes, no functions)

Non-type Parameters

Pointers

References

Integral constant expressions

What you have there is of the last kind. It's a compile time constant (so-called constant expression) and is of type integer or enumeration. After looking it up in the standard, i had to move class templates up into the types section - even though templates are not types. But they are called type-parameters for the purpose of describing those kinds nonetheless. You can have pointers (and also member pointers) and references to objects/functions that have external linkage (those that can be linked to from other object files and whose address is unique in the entire program). Examples:

 

Template type parameter:

 

template<typename T>

struct Container {

    T t;

};

 

// pass type "long" as argument.

Container<long> test;

Template integer parameter:

 

template<unsigned int S>

struct Vector {

    unsigned char bytes[S];

};

 

// pass 3 as argument.

Vector<3> test;

Template pointer parameter (passing a pointer to a function)

 

template<void (*F)()>

struct FunctionWrapper {

    static void call_it() { F(); }

};

 

// pass address of function do_it as argument.

void do_it() { }

FunctionWrapper<&do_it> test;

Template reference parameter (passing an integer)

 

template<int &A>

struct SillyExample {

    static void do_it() { A = 10; }

};

 

// pass flag as argument

int flag;

SillyExample<flag> test;

Template template parameter.

 

template<template<typename T> class AllocatePolicy>

struct Pool {

    void allocate(size_t n) {

        int *p = AllocatePolicy<int>::allocate(n);

    }

};

 

// pass the template "allocator" as argument. 

template<typename T>

struct allocator { static T * allocate(size_t n) { return 0; } };

Pool<allocator> test;

A template without any parameters is not possible. But a template without any explicit argument is possible - it has default arguments:

 

template<unsigned int SIZE = 3>

struct Vector {

    unsigned char buffer[SIZE];

};

 

Vector<> test;

Syntactically, template<> is reserved to mark an explicit template specialization, instead of a template without parameters:

 

template<>

struct Vector<3> {

    // alternative definition for SIZE == 3

};

分享到:
评论

相关推荐

    ArduinoJson:for用于Arduino和嵌入式C ++的JSON库。 简单高效

    ArduinoJson是一个用于Arduino和IoT(物联网)的C ++ JSON库。 特征 支持单引号作为字符串定界符 与和兼容 高效的 对字符串进行重复数据删除 多才多艺的 支持和 支持和 ...可用于任何C ++项目(不限于Arduino) ...

    C++ Templates 完全导引

    模板的递归使用也需要理解,如在实现阶乘函数`template&lt;unsigned int N&gt; int factorial()`时,模板会递归展开为`factorial&lt;N-1&gt;() * N`,直到达到基本情况(N=1)。 模板的依赖性问题也是C++模板编程的一大挑战。当...

    哈希表(带template)

    reinterpret_cast&lt;const unsigned char *&gt;(_First), _Count * sizeof(_Kty))); } /*hash_val(string)*/ template&lt;class _Elem, class _Traits, class _Alloc&gt; inline size_t hash_val(const basic_string&lt;_...

    ANSI-ISO关于模板的定义

    const T& Vector&lt;T&gt;::operator[](unsigned int i) const { return buff[i]; } template&lt;typename T&gt; size_t Vector&lt;T&gt;::size() const { return sz; } ``` ##### 成员函数外部定义 成员函数可以在类体之外定义,...

    c++实现二叉查找树

    template&lt;class T&gt; class node { public: node() { lchild = rchild = NULL; } T data; node&lt;T&gt;* lchild, *rchild; }; ``` ##### 2. `BST` 类 用于表示二叉查找树,包含了多个成员函数用于操作二叉查找树。 - `...

    第七次上机题目及参考代码 (1).docx

    SeqList&lt;DataType&gt;::SeqList(DataType a[], int n) { len = n; for (int i = 0; i &lt; len; ++i) arr[i] = a[i]; } template &lt;typename DataType&gt; SeqList&lt;DataType&gt;::SeqList(const SeqList &s) { len = s.len; ...

    phpwind 4.3.2 单独增加数据库分卷分表功能

    &lt;td class=b&gt;&lt;select name=fid&gt;&lt;option value='-1'&gt;所有版块&lt;/option&gt;$forumcache&lt;/select&gt;&lt;/td&gt;&lt;/tr&gt; 三行的下面增加 &lt;!-- EOT; if($p_table){ print &lt;&lt;&lt;EOT --&gt; &lt;tr&gt;&lt;td class=b&gt;分表数据库&lt;/td&gt; ...

    STL各种函数详细讲解

    typedef pair&lt;unsigned long, int&gt; node_type; int main() { priority_queue&lt;node_type, vector&lt;node_type&gt;, greater&lt;node_type&gt;&gt; Q; Q.push(make_pair(1, 2)); for (int i = 0; i &lt; 1500; i++) { node_type ...

    C++模板与STL学习

    这使得我们能够使用同一个类实现多种类型的栈,如 `Stack&lt;int&gt;` 和 `Stack&lt;double&gt;`。 ##### 方法定义 模板类的方法定义也需要包含模板声明: ```cpp template&lt;typename T&gt; bool Stack&lt;T&gt;::push(T t) { if (top ...

    用VC++进行图像处理中的几点技巧与经验

    template&lt;class T&gt; class ImageTemplate { public: T** lp_AddRow; // 数据指针 unsigned int Width; // 宽度 unsigned int Height; // 高度 unsigned int ImageSize; // 图像大小 ImageTemplate(); ~...

    standard template library 基础知识

    for (std::list&lt;int&gt;::iterator it = intList.begin(); it != intList.end(); ++it) { std::cout &lt;&lt; *it &lt;&lt; " "; } ``` 这个例子展示了如何向`list`中添加元素以及遍历`list`的元素。 **STL算法库** STL还提供了...

    c++中template对字符串的处理方法

    template &lt;&gt; inline int hash_wrap&lt;string&gt;(const string & k) { return k.size(); } ``` 另外两个例子展示了如何对其他模板函数进行特化。第一个例子`get_size`,常规模板计算类型`Kty`的大小,而特化版本则返回`...

    第九章 模板(计算机等级考试二级).pdf

    addTo&lt;&gt;(x, y, 9); ``` ### 类模板 #### 类模板的声明和使用 类模板和函数模板类似,它定义了一类具有相同成员组成但成员函数针对不同数据类型的类。类模板在声明时需要指定类型参数,这些类型参数在类实例化时被...

    vectorize:SSEAVX

    它支持以下功能:(T:float / double) // dst[i] += c * src[i]template&lt;typename&gt;void muladd(const T* src, T c, unsigned int size, T* dst)// dst = sum(s1[i] * s2[i])template&lt;typename&gt;T dot(const T* s1, ...

    栈与递归PPT学习教案.pptx

    unsigned int factorial(unsigned int n) { if (n == 0) return 1; else return n * factorial(n - 1); } ``` 递归在编程中广泛应用,特别是在数据结构如树和图的处理中。例如,链表的搜索操作可能需要递归地...

    格雷码生成程序

    vector&lt;string&gt; ProducetNGray(unsigned n) { vector&lt;string&gt; Ans; // ... return Ans; } void PrintNGray(vector&lt;string&gt; gray) { int m = (int)gray.size(); // ... } ``` 四、应用 格雷码广泛应用于数字...

    基于boost的序列化与反序列化

    template &lt;typename Archive&gt; void serialize(Archive &ar, const unsigned int version) { ar & myVar1; ar & myVar2; // ... } }; ``` 反序列化的过程则是将字节流还原为对象。使用`archive::text_iarchive...

    c与c++规范

    - `&lt;tgmath.h&gt;`和`&lt;math.h&gt;`:这两个头文件在C++11中提供了数学函数,`&lt;tgmath.h&gt;`提供类型通用的数学函数,`&lt;math.h&gt;`则包含传统C语言的数学函数。 - `&lt;iconv&gt;`和`tm`:`&lt;iconv&gt;`涉及字符集转换,`tm`是时间...

    thl_r16_tinav2.0_hm1375验证通过_增加打印设备ID_20170824_1447.7z

    dev-&gt;dev_act[input_num].addr = (unsigned short)(dev-&gt;ccm_cfg[input_num]-&gt;act_slave&gt;&gt;1); strcpy(dev-&gt;dev_act[input_num].type,dev-&gt;ccm_cfg[input_num]-&gt;act_name); if(vfe_actuator_subdev_register...

Global site tag (gtag.js) - Google Analytics