c语言中int n的意思,c ++ - 模板<unsigned int N>是什么意思?

本文详细介绍了C++模板的参数类型,包括类型参数、整数参数、指针参数、引用参数以及模板模板参数。通过示例展示了如何使用这些参数,并解释了它们在编译时常量表达式中的应用。同时,讨论了无参数模板的可能性以及显式模板特化的语法。
摘要由CSDN通过智能技术生成

是的,它是一个非类型参数。 您可以拥有多种模板参数

类型参数。类型

模板(只有类和别名模板,没有函数或变量模板)

非类型参数指针

参考

积分常量表达式

你拥有的是最后一种。 它是一个编译时常量(所谓的常量表达式),是整数或枚举类型。 在标准中查找之后,我不得不将类模板移动到类型部分 - 即使模板不是类型。 但它们被称为类型参数,目的是为了描述这些类型。 您可以拥有指针(以及成员指针)和对具有外部链接的对象/函数的引用(可以链接到其他目标文件并且其地址在整个程序中是唯一的)。 例子:

模板类型参数:

template

struct Container {

T t;

};

// pass type "long" as argument.

Container test;

模板整数参数:

template

struct Vector {

unsigned char bytes[S];

};

// pass 3 as argument.

Vector<3> test;

模板指针参数(将指针传递给函数)

template

struct FunctionWrapper {

static void call_it() { F(); }

};

// pass address of function do_it as argument.

void do_it() { }

FunctionWrapper test;

模板引用参数(传递整数)

template

struct SillyExample {

static void do_it() { A = 10; }

};

// pass flag as argument

int flag;

SillyExample test;

模板模板参数。

template class AllocatePolicy>

struct Pool {

void allocate(size_t n) {

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

}

};

// pass the template "allocator" as argument.

template

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

Pool test;

不能使用没有任何参数的模板。 但是没有任何显式参数的模板是可能的 - 它有默认参数:

template

struct Vector {

unsigned char buffer[SIZE];

};

Vector<> test;

从语法上讲,保留template<>来标记显式模板特化,而不是没有参数的模板:

template<>

struct Vector<3> {

// alternative definition for SIZE == 3

};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值