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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 张瓅

puppy-language ConsolePlayer 仙剑奇侠传4主题曲 QBASIC代码 HTML5 模版调用析构函数的一个发现 如何测量代码执行时间 【转】老外眼中的武侠小说 1850年的宁波 关于安装 DirectX SDk Dec 2005 后无法编译DirectShow应用程序的问题 [转]VB中嵌入汇编与真正的DLL 新的连连看网络版 各种连接字符串 关于CSS属性display:none和visible:hidden的区别 我用超白痴的方法解出了这道题,大家有没有更好的方法 Fire Net 尝试了一下Flex VFP中如何调用API函数 DES加密算法的实现 软件项目经理必备素质(转)
编译期运算
张瓅 · 2006-11-18 · via 博客园 - 张瓅

最近在研究C++模版,发现一个以前没有遇到过的技术,就是编译期计算。
所谓编译期运算就是指在编译阶段由编译器所进行的运算,不占用运行期时间,有时也称元模版编程。
所有可以在编译期决定的数值都可以用这种方法解决以节省运行时的浪费,只是这种方法毫无灵活性可言,平时用得很少,仅了解一下即可。
以下运算不消耗运行时,全部数值计算在编译时就已经完成。

#include <iostream>

template 
<int _P, int _Q = _P>
class IsPrime
{
public:
    
enum{result = (_P % (_Q - 1) ) && IsPrime<_P, _Q - 1>::result };
}
;

template 
<int _P>
class IsPrime<_P, 2>
{
public:
    
enum{ result = 1};
}
;

template
<bool C, typename Ta, typename Tb>
class IfThenElse
{
    
public:
        typedef Tb ResultT;
}
;

template
<typename Ta, typename Tb>
class IfThenElse<true, Ta, Tb>
{
public:
    typedef Ta ResultT;
}
;

template 
<int _P, int _Q = 0>
class CountPrime
{
public:
    typedef typename IfThenElse
<IsPrime<_P>::result,
        CountPrime
<_P - 1, _Q + 1>,
        CountPrime
<_P - 1, _Q> >::ResultT
        SubT;
    
enum { result = SubT::result };
}
;

template 
<int _Q>
class CountPrime<2, _Q>
{
public:
    
enum{result = _Q + 1 };
}
;

int main()
{
    std::cout 
<< IsPrime<33>::result << std::endl;    //判断素数
    std::cout << IsPrime<11>::result << std::endl;
    std::cout 
<< IsPrime<7>::result << std::endl;
    std::cout 
<< IsPrime<179>::result << std::endl;
    std::cout 
<< CountPrime<100>::result << std::endl;    //统计x以内素数

    system(
"pause");
}

另外还可以用这个技术写出其他的运算函数,不过代码既不灵活,对模版不熟悉的人来说又晦涩难以维护,所以不太推荐。

真正把模版用得出神入化而且非常实用的是Loki库,大家可以参考《Morded C++ Design》这本书,第一次看到这本书的时候,让我大大地开了眼界,以至对C++产生了陌生感。