- 浏览: 208493 次
- 性别:
- 来自: 重庆
-
文章分类
最新评论
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
};
发表评论
-
C++的原子操作
2012-12-20 17:43 4697在多进程(线程)访问资源时,能够确保所有其他的进程(线程 ... -
匿名namespace的作用以及它与static的区别
2012-12-20 17:24 1865一。匿名namespace的作用 在C语言中,如果我们 ... -
C++类型萃取技术
2012-12-19 15:16 1158Traits技术可以用来获得一个 类型 的相关信息的。 ... -
数值压缩存储方法Varint
2012-12-19 14:35 881转自:http://www.cnblogs.com/smark ... -
TypeList
2012-12-19 13:49 1163转自:http://blog.csdn.n ... -
二维指针*(void **)的研究(uC/OS-II案例)
2012-12-19 22:20 3308原文 : http://blog.csdn ... -
多级指针和链表
2012-12-18 22:28 0如果看到一个声明:t ... -
理解*(void**)b
2012-12-18 22:03 0#include <stdio.h> ... -
STL标准库:Allocator能做什么
2012-12-18 20:10 0The Standard Librarian: Wha ... -
三种的allocator实现源代码的对比
2012-12-18 19:55 1353转自:http://blog.csdn.net ... -
结构体内变量相对便宜与list_entry()宏
2012-12-18 17:59 963#define list_entry(ptr, t ... -
声明与函数、函数指针---(*(void (*)( ) )0)( ) 解析
2012-12-18 17:33 1123概述 在很 ... -
c++模板(类型依赖)说明例子
2012-12-18 16:57 1173#include <iostream> # ... -
C++中三种new的用法
2012-12-18 16:44 1845我评价自己的C++水平还未入门的确不够准确,应该是远远未 ... -
C++,永久改变你写异常安全代码的方式(神奇的Loki::ScopeGuard)
2012-12-17 20:19 2528作者:Andrei Alexandrescu and P ... -
C++的make_pair函数
2012-12-17 17:19 3538Pairs C++标准程序库中凡是“必须返回两 ... -
C++的explicit构造函数
2012-12-13 15:59 684按照默认规定,只有一个参数的构造函数也定义了一个隐式转换 ...
相关推荐
ArduinoJson是一个用于Arduino和IoT(物联网)的C ++ JSON库。 特征 支持单引号作为字符串定界符 与和兼容 高效的 对字符串进行重复数据删除 多才多艺的 支持和 支持和 ...可用于任何C ++项目(不限于Arduino) ...
模板的递归使用也需要理解,如在实现阶乘函数`template<unsigned int N> int factorial()`时,模板会递归展开为`factorial<N-1>() * N`,直到达到基本情况(N=1)。 模板的依赖性问题也是C++模板编程的一大挑战。当...
reinterpret_cast<const unsigned char *>(_First), _Count * sizeof(_Kty))); } /*hash_val(string)*/ template<class _Elem, class _Traits, class _Alloc> inline size_t hash_val(const basic_string<_...
const T& Vector<T>::operator[](unsigned int i) const { return buff[i]; } template<typename T> size_t Vector<T>::size() const { return sz; } ``` ##### 成员函数外部定义 成员函数可以在类体之外定义,...
template<class T> class node { public: node() { lchild = rchild = NULL; } T data; node<T>* lchild, *rchild; }; ``` ##### 2. `BST` 类 用于表示二叉查找树,包含了多个成员函数用于操作二叉查找树。 - `...
SeqList<DataType>::SeqList(DataType a[], int n) { len = n; for (int i = 0; i < len; ++i) arr[i] = a[i]; } template <typename DataType> SeqList<DataType>::SeqList(const SeqList &s) { len = s.len; ...
<td class=b><select name=fid><option value='-1'>所有版块</option>$forumcache</select></td></tr> 三行的下面增加 <!-- EOT; if($p_table){ print <<<EOT --> <tr><td class=b>分表数据库</td> ...
typedef pair<unsigned long, int> node_type; int main() { priority_queue<node_type, vector<node_type>, greater<node_type>> Q; Q.push(make_pair(1, 2)); for (int i = 0; i < 1500; i++) { node_type ...
这使得我们能够使用同一个类实现多种类型的栈,如 `Stack<int>` 和 `Stack<double>`。 ##### 方法定义 模板类的方法定义也需要包含模板声明: ```cpp template<typename T> bool Stack<T>::push(T t) { if (top ...
template<class T> class ImageTemplate { public: T** lp_AddRow; // 数据指针 unsigned int Width; // 宽度 unsigned int Height; // 高度 unsigned int ImageSize; // 图像大小 ImageTemplate(); ~...
for (std::list<int>::iterator it = intList.begin(); it != intList.end(); ++it) { std::cout << *it << " "; } ``` 这个例子展示了如何向`list`中添加元素以及遍历`list`的元素。 **STL算法库** STL还提供了...
template <> inline int hash_wrap<string>(const string & k) { return k.size(); } ``` 另外两个例子展示了如何对其他模板函数进行特化。第一个例子`get_size`,常规模板计算类型`Kty`的大小,而特化版本则返回`...
addTo<>(x, y, 9); ``` ### 类模板 #### 类模板的声明和使用 类模板和函数模板类似,它定义了一类具有相同成员组成但成员函数针对不同数据类型的类。类模板在声明时需要指定类型参数,这些类型参数在类实例化时被...
它支持以下功能:(T:float / double) // dst[i] += c * src[i]template<typename>void muladd(const T* src, T c, unsigned int size, T* dst)// dst = sum(s1[i] * s2[i])template<typename>T dot(const T* s1, ...
unsigned int factorial(unsigned int n) { if (n == 0) return 1; else return n * factorial(n - 1); } ``` 递归在编程中广泛应用,特别是在数据结构如树和图的处理中。例如,链表的搜索操作可能需要递归地...
vector<string> ProducetNGray(unsigned n) { vector<string> Ans; // ... return Ans; } void PrintNGray(vector<string> gray) { int m = (int)gray.size(); // ... } ``` 四、应用 格雷码广泛应用于数字...
template <typename Archive> void serialize(Archive &ar, const unsigned int version) { ar & myVar1; ar & myVar2; // ... } }; ``` 反序列化的过程则是将字节流还原为对象。使用`archive::text_iarchive...
- `<tgmath.h>`和`<math.h>`:这两个头文件在C++11中提供了数学函数,`<tgmath.h>`提供类型通用的数学函数,`<math.h>`则包含传统C语言的数学函数。 - `<iconv>`和`tm`:`<iconv>`涉及字符集转换,`tm`是时间...
dev->dev_act[input_num].addr = (unsigned short)(dev->ccm_cfg[input_num]->act_slave>>1); strcpy(dev->dev_act[input_num].type,dev->ccm_cfg[input_num]->act_name); if(vfe_actuator_subdev_register...