C ++模板元编程的最佳介绍? [英] Best introduction to C++ template metaprogramming?

查看:143
本文介绍了C ++模板元编程的最佳介绍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

静态元程序设计(akatemplate metaprogramming)是一个伟大的C ++技术,允许在编译时执行程序。一旦我读到这个规范的元编程示例,一个灯泡就在我的头上了。

Static metaprogramming (aka "template metaprogramming") is a great C++ technique that allows the execution of programs at compile-time. A light bulb went off in my head as soon as I read this canonical metaprogramming example:

#include <iostream>
using namespace std;

template< int n >
struct factorial { enum { ret = factorial< n - 1 >::ret * n }; };

template<>
struct factorial< 0 > { enum { ret = 1 }; };

int main() {
    cout << "7! = " << factorial< 7 >::ret << endl; // 5040
    return 0;
}

如果想要了解更多关于C ++静态元编程的知识,

If one wants to learn more about C++ static metaprogramming, what are the best sources (books, websites, on-line courseware, whatever)?

推荐答案

[回答自己的问题]

到目前为止,我发现的最好的介绍是第10章C ++中的静态元程序设计,来自生成式编程,方法,工具和应用程序< emz by Krzysztof Czarnecki和Ulrich W.Eisenecker,ISBN-13:9780201309775;以及David Vandevoorder和Nicolai M. Josuttis,ISBN-13:9780201734843的 C ++模板:完整指南的第17章元程序。

The best introductions I've found so far are chapter 10, "Static Metaprogramming in C++" from Generative Programming, Methods, Tools, and Applications by Krzysztof Czarnecki and Ulrich W. Eisenecker, ISBN-13: 9780201309775; and chapter 17, "Metaprograms" of C++ Templates: The Complete Guide by David Vandevoorder and Nicolai M. Josuttis, ISBN-13: 9780201734843.

Todd Veldhuizen有一个很好的教程此处

Todd Veldhuizen has an excellent tutorial here.

对于C ++编程,一个好的资源通常是来自Andrei Alexandrescu的现代C ++设计,ISBN-13:9780201704310.这本书将一些元编程与其他模板技术相结合。特别是对于元编程,请参见2.1编译时断言,2.4映射积分常数到类型,2.6类型选择,2.7检测编译时的可转换性和继承,2.9 NullType EmptyType 和2.10类型性格。

A good resource for C++ programming in general is Modern C++ Design by Andrei Alexandrescu, ISBN-13: 9780201704310. This book mixes a bit of metaprogramming with other template techniques. For metaprogramming in particular, see sections 2.1 "Compile-Time Assertions", 2.4 "Mapping Integral Constants to Types", 2.6 "Type Selection", 2.7 "Detecting Convertibility and Inheritance at Compile Time", 2.9 "NullType and EmptyType" and 2.10 "Type Traits".

资源我找到的是David Abrahams和Aleksey Gurtovoy,ISBN-13:9780321227256

The best intermediate/advanced resource I've found is C++ Template Metaprogramming by David Abrahams and Aleksey Gurtovoy, ISBN-13: 9780321227256

如果你只想要一个book,get< em> C ++ Templates:The Complete Guide ,因为它也是一般模板的确切参考。

If you'd prefer just one book, get C++ Templates: The Complete Guide since it is also the definitive reference for templates in general.

这篇关于C ++模板元编程的最佳介绍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆