`

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

};

分享到:
评论

相关推荐

    哈希表(带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;_...

    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; ...

    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, ...

    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...

    hm1375_tinav2.1验证通过_增加设备ID的读取显示_20170825_1333没有外层目录.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...

    gh0st的socket内核文件

    template&lt;&gt; inline UINT AFXAPI HashKey(CString & strGuid) { return HashKey( (LPCTSTR) strGuid); } #include "Mapper.h" typedef void (CALLBACK* NOTIFYPROC)(LPVOID, ClientContext*, UINT nCode); ...

    嵌入式实验(消息队列)

    template&lt;T&gt; T* mymalloc(unsigned nBytes); void myfree(void); void bye(void); /***************************************[progStart]*******************************************/ /*启动程序,创建息队例,...

    menyf#acm-icpc-template#字符串hash1

    /简单的字符串hash/unsigned int hash = 0;return (hash & 0x7FFFFFFF);

    phpwind 4.3.2远程ftp上传附件功能(附件存另一个服务器)

    ... job.php,read.php,show.php,setforum.php,postupload.php,template.php,setforum.htm 1. 数据库升级 进入phpMyAdmin -&gt; SQL... &lt;td&gt;&lt;select name="remoteftp"&gt;$ftpselect&lt;/select&gt;&lt;/td&gt; &lt;/tr&gt; (完成...)

    stdafx.h代码

    #include &lt;afxres.h&gt; // standard resource IDs #endif #ifndef __AFXCOLL_H__ #include &lt;afxcoll.h&gt; // standard collections #endif #ifdef _AFX_MINREBUILD #pragma component(minrebuild, off) #endif #...

    phpCMS数据库设计.doc

    " " " " " " "2 "placename "char(50) "NO " "" 广告位名称 " "3 "template "char(30) "NO " "0 " 模板 " "4 "introduce "char(100) "NO " "" 广告位介绍 " "5 "price "mediumint(8) unsigned "NO " "0 " 广告价格 ...

    Bezier曲线类的构造

    void resize( int new-ncols, int new-nrows) ; friend Mat rixT emplate3T4 operator * ( Mat rixTemplate3T4 & , Mat rixTemplate3T4 & ) ; friend VectorTemplate3T4 operator * ( MatrixTemplate3T4 & , ...

    ecSHOP模板包

    (159, '首页左侧广告', 216, 172, '', '&lt;table cellpadding="0" cellspacing="0"&gt;\r\n{foreach from=$ads item=ad}\r\n&lt;tr&gt;&lt;td&gt;{$ad}&lt;/td&gt;&lt;/tr&gt;\r\n{/foreach}\r\n&lt;/table&gt;'); -- --------------------------------...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    The format of the symbol name should be &lt;PROJECT&gt;_&lt;PATH&gt;_&lt;FILE&gt;_H_. To guarantee uniqueness, they should be based on the full path in a project's source tree. For example, the file foo/src/bar/baz.h...

    template-turing:C ++模板图灵机

    state.h包含一个琐碎的模板,用于包装unsigned int来形成机器状态。 还定义了HALT 。 colour.h是另一个包装char的琐碎模板:这些char代表各种磁带单元的颜色。 EMPTY在这里定义。 direction.h定义了三种类型: go...

    C++中的函数修饰符深入讲解

    返回值类型是C++中定义函数的必备部分,这些修饰符包括void,(unsigned) int,bool等内置基本数据类型和自定义类型,也包括修饰返回值const关键字(如const int*),还包括C++11中新增的类型自动推导auto关键字。...

    C++基本概念 (面试、复习)

    auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while ...

    C++关键字详细使用

    goto if inline int long mutable namespace new operator private protected public register reinterpret_cast return short signed sizeof static static_cast struct switch template this throw true try type_...

    C++关键字大全(67个).txt

    int long mutable namespace new operator private protected public register reinterpret_cast return short signed sizeof static static_cast struct switch template this throw true try type_info typedef ...

    数位板压力测试

    • Tablets often control the system cursor, provide additional digitizing input, and provide template or macro functions. 1.2 The Windows Environment Programming for tablets in the Windows ...

Global site tag (gtag.js) - Google Analytics