C++ 模板元编程的最佳介绍?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/112277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best introduction to C++ template metaprogramming?
提问by jwfearn
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:
静态元编程(又名“模板元编程”)是一种很棒的 C++ 技术,它允许在编译时执行程序。当我读到这个规范的元编程示例时,一个灯泡在我脑海中熄灭了:
#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;
}
If one wants to learn more about C++ static metaprogramming, what are the best sources (books, websites, on-line courseware, whatever)?
如果想了解更多关于 C++ 静态元编程的知识,最好的来源是什么(书籍、网站、在线课件等等)?
采纳答案by jwfearn
[Answering my own question]
[回答我自己的问题]
The best introductions I've found so far are chapter 10, "Static Metaprogramming in C++" from Generative Programming, Methods, Tools, and Applicationsby Krzysztof Czarnecki and Ulrich W. Eisenecker, ISBN-13: 9780201309775; and chapter 17, "Metaprograms" of C++ Templates: The Complete Guideby David Vandevoorder and Nicolai M. Josuttis, ISBN-13: 9780201734843.
到目前为止,我发现的最好的介绍是第 10 章“C++ 中的静态元编程”,来自Krzysztof Czarnecki 和 Ulrich W. Eisenecker 的Generative Programming, Methods, Tools, and Applications,ISBN-13:9780201309775;和第 17 章,C++ 模板的“元程序” :David Vandevoorder 和 Nicolai M. Josuttis的完整指南,ISBN-13:9780201734843。
Todd Veldhuizen has an excellent tutorial here.
Todd Veldhuizen在这里有一个很好的教程。
A good resource for C++ programming in general is Modern C++ Designby 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++ 编程的一个很好的资源是Andrei Alexandrescu 的Modern C++ Design,ISBN-13:9780201704310。这本书混合了一些元编程和其他模板技术。特别是对于元编程,请参阅第 2.1 节“编译时断言”、2.4“将积分常量映射到类型”、2.6“类型选择”、2.7“在编译时检测可转换性和继承性”、2.9“NullType
和EmptyType
”和 2.10“类型特征” ”。
The best intermediate/advanced resource I've found is C++ Template Metaprogrammingby David Abrahams and Aleksey Gurtovoy, ISBN-13: 9780321227256
我发现的最好的中级/高级资源是David Abrahams 和 Aleksey Gurtovoy 的C++ 模板元编程,ISBN-13:9780321227256
If you'd prefer just one book, get C++ Templates: The Complete Guidesince it is also the definitive reference for templates in general.
如果您只喜欢一本书,请获取C++ 模板:完整指南,因为它也是一般模板的权威参考。
回答by Alaric
Andrei Alexandrescu's Modern C++ Designbook covers a lot of this and other tricks for speedy and efficient modern C++ code and is the basis for the Lokilibrary.
Andrei Alexandrescu 的《现代 C++ 设计》一书涵盖了许多有关快速高效的现代 C++ 代码的技巧和其他技巧,并且是Loki库的基础。
Also worth mentioning is the Boostlibraries, which heavily use these techniques and are usually of very high quality to learn from (although some are quite dense).
另外值得一提的是Boost库,它们大量使用这些技术并且通常具有非常高的学习质量(尽管有些非常密集)。
回答by Florian B?sch
Modern C++ Design, a brilliant book and design pattern framework by Alexandrescu. Word of warning, after reading this book I stopped doing C++ and thought "What the heck, I can just pick a better language and get it for free".
Modern C++ Design,一本精彩的书和 Alexandrescu 的设计模式框架。警告的话,读完这本书后,我停止使用 C++ 并想“这到底是什么,我可以选择一种更好的语言并免费获得它”。
回答by Nik
Two good books that spring to mind are:
想到的两本好书是:
- Modern C++ Design / Andrei Alexandrescu (It's actually 7 years old despite the name!)
- C++ Templates: The Complete Guide / Vandevoorde & Josuttis
- 现代 C++ 设计 / Andrei Alexandrescu(尽管它的名字实际上已经 7 岁了!)
- C++ 模板:完整指南 / Vandevoorde & Josuttis
It's quite an in-depth field, so a good book like one of these is definitely recommended over websites. Some of the more advanced techniques will have you studying the code for some time to figure out how they work!
这是一个非常深入的领域,因此肯定会在网站上推荐像其中一本这样的好书。一些更高级的技术会让您研究代码一段时间,以弄清楚它们是如何工作的!
回答by MSalters
There won't be a large list of books, as the list of people with a lot of experience is limited. Template metaprogramming started for real around the first C++ Template Programming Workshop in 2000, and many of the authors named so far attended. (IIRC, Andrei didn't.) These pioneers greatly influenced the field, and basically what should be written is now written. Personally, I'd advice Vandevoorde & Josuttis. Alexandrescu's is a tough book if you're new to the field.
不会有很大的书单,因为有很多经验的人的名单是有限的。模板元编程在 2000 年的第一届 C++ 模板编程研讨会上真正开始了,迄今为止的许多作者都参加了。(IIRC,Andrei 没有。)这些先驱极大地影响了该领域,基本上应该写的东西现在写了。就个人而言,我建议 Vandevoorde & Josuttis。如果您是该领域的新手,Alexandrescu 的书很难读。
回答by Eclipse
Modern C++is one of the best introductions I've read. It covers actual useful examples of template metaprogramming. Also take a look at the companion library Loki.
Modern C++是我读过的最好的介绍之一。它涵盖了模板元编程的实际有用示例。还可以看看Loki的伴侣库。
回答by eduffy
Veldhuizen's original papers were good. If you up for a whole book, then there's Vandevoorde's book "C++ Templates Complete Guide". And when you're ready for the master's course, try Alexandrescu's Modern C++ Design.
Veldhuizen 的原始论文很好。如果你想读一整本书,那么就有 Vandevoorde 的书“C++ 模板完整指南”。当您准备好学习硕士课程时,请尝试 Alexandrescu 的现代 C++ 设计。
回答by Maxim Ananyev
google Alexandrescu, Modern C++ Design: Generic Programming and Design Patterns Applied
google Alexandrescu,现代 C++ 设计:应用的通用编程和设计模式