静态元编程(又名模板元编程")是一种很棒的 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++ 中的静态元编程",来自生成式编程、方法、工具和应用程序,作者是 Krzysztof Czarnecki 和 Ulrich W. Eisenecker,ISBN-13:9780201309775;和第 17 章元程序",C++ 模板:完整指南 作者:David Vandevoorder 和 Nicolai M. Josuttis,ISBN-13:9780201734843.
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++ 编程的一个很好的资源是Modern C++ Design,作者是 Andrei Alexandrescu,ISBN-13:9780201704310.这本书混合了一些元编程和其他模板技术.对于元编程,请参见第 2.1 节编译时断言"、2.4将积分常量映射到类型"、2.6类型选择"、2.7在编译时检测可转换性和继承性"、2.9NullType
和 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".
我发现的最好的中级/高级资源是 C++ 模板元编程,作者是 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
如果您只喜欢一本书,请获取C++ 模板:完整指南,因为它也是一般模板的权威参考.
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++ 模板元编程的最佳介绍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!