惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Blog — PlanetScale
Blog — PlanetScale
博客园 - Franky
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
D
Docker
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
N
Netflix TechBlog - Medium
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
H
Help Net Security
B
Blog
宝玉的分享
宝玉的分享

Victrid's Personal Site

斯普拉遁3打工模式联机网络过程分析 DHCP无类型静态路由──一个摆设 配置透明代理,实现无感上网 Generating Unlimited Grammar: A Messenger Perspective LaTeX插入其他PDF中的矢量图 光速不变原理和迈克尔孙──莫雷实验 交大葡萄演义 调试微信内置浏览器 欧悌甫戎篇 苏格拉底的申辩篇 LCA 最近公共祖先 弥罗斯人的辩论 埃斯库罗斯作品:波斯人、阿伽门农 电路理论资料 Coronavirus: A Humanitarian Crisis 雨夜 linux配置SSR 二叉堆的实现 懒政 Placement New Flipped: A Love Story 我宁可呆在这样的疯人院里 谈新发本地病例 勇敢与智慧 Powerful but Limited Drafted VLC Libva Error Troubleshooting Ring-Fit新上手 政治艺术化与艺术政治化 动态规划——从分割等和子集入手
非类型模板参数
Victrid · 2020-01-17 · via Victrid's Personal Site

在泛型的学习中我们用到了类型模板。例如以下程序片段:

1
2
3
4
5
6
7
8
9
10
template<typename T>
class foo{
T bar;
...
}

template<typename S>
S fec(S arg){
...
}

但是template的用法不止于此。还有一种非类型模板。这种方法可以减小程序的开销,增强代码的复用性。

例如以下程序片段:

1
2
3
4
template<typename C,int N>
void sort(C (&pos)[N]){
...
}

这个方法可以完整地传入一个数组,而不是退化的指针,包括数组长度都被传入,这样有利于预防数组越界的问题。

当然,这样的方法只允许我们传入一个数组名,而不是首地址指针,因为template的实例化是在预编译时完成的,数组必须是固定长度申请的,因此c++的新特性,即变量长度定义的数组在这里不适用。

由此看来,typename和nullptr有一种设计哲学上的共性,他们通过对全体类型的抽象,而在使用时隐式地转换为所调用的具体类型。相同的,他们的实现都是在预编译的过程中实现的,其本身只是语法上的助记符而已。

Author: Victrid

Permanent Link: https://victrid.dev/2020/fei-lei-xing-mo-ban-can-shu/

License: Copyright (c) 2025 victrid Terms of Use

Ads by Google

Read our privacy policy on how these personalized advertisements are delivered to you.

For your reading experience, we provide full-text RSS feeds. Although math formulas cannot be displayed well, the interface can be adjusted as you like and there are no ads.