問題描述
靜態元編程(又名模板元編程")是一種很棒的 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模板網!