C ++模板的好点和坏点是什么? [英] What are the good and bad points of C++ templates?
问题描述
我一直在和朋友交谈,有些人完全同意应该使用C ++中的模板,其他人完全不同意。
一些好东西是: p>
- 他们更安全使用(类型安全)。
您可以告诉我什么其他有趣的C ++范本?
有什么不好的事情可以告诉我关于C ++模板?
编辑:我问这个问题的原因之一是,我正在考试,这一刻我覆盖了C ++模板的主题。
模板是一个非常强大的机制,可以简化许多事情。
对我来说,最重要的优势是:
- 减少重复的代码(通用容器,算法)
- 减少代码高级(MPL和Fusion) / li>
- 静态多态性(=性能)和其他编译时计算
- 基于策略的设计(灵活性,可重用性, b $ b
- 功能编程(Phoenix),延迟评估,表达式模板(我们可以通过Boost单元进行维度分析,静态断言,概念检查)在C ++中创建领域专用的嵌入式语言,我们有伟大的Proto库,我们有Blitz ++)
- 其他不太壮观的工具和技巧用于日常生活:
- STL和算法(
for_each
之间的区别) - bind,lambda(或Phoenix)(编写更清晰的代码,简化操作)
- Boost函数元组(如何通用哈希一个元组?使用Fusion例如...)
- TBB(
parallel_for
和其他STL类似算法和容器)
- STL和算法(
- 你能想象C ++没有模板吗?是的,我可以,在早期,你不能使用它们,因为编译器的限制。
- 你会用C ++编写没有模板?不,我会失去上述许多优点。
缺点:
- 编译时间(例如投掷Sprit,Phoenix,MPL和一些Fusion,您可以去喝咖啡)
- 使用和了解模板并不常见(这些人很有用)
- 人们认为他们可以使用和理解模板是很常见的(这些人是危险的,因为他们可以让你的代码,但他们大部分后一些教育/指导将加入上述提到的小组)
- template
export
支持(缺少) - 错误消息可能不那么神秘(在学习之后,您可以找到所需的内容, 。)
我强烈推荐以下书籍:
- C ++模板:David Vandevoorde和Nicolai Josuttis的完整指南(全面介绍模板主题)
- 现代C ++设计。通用编程和设计模式由Andrei Alexandrescu应用(什么是使用模板简化你的代码,使开发更容易,导致代码强大的变化更少知名的方式)
- David Abrahms和Aleksey Gutov的C ++模板元编程(再次 - 使用模板的不同方式)
- 维基教科书中的更多C ++成语提供了一些不错的想法。 li>
I've been talking with friends and some completely agree that templates in C++ should be used, others disagree entirely.
Some of the good things are:
- They are more safe to use (type safety).
- They are a good way of doing generalizations for APIs.
What other good things can you tell me about C++ templates?
What bad things can you tell me about C++ templates?
Edit: One of the reasons I'm asking this is that I am studying for an exam and at the moment I am covering the topic of C++ templates. So I am trying to understand a bit more on them.
Templates are a very powerful mechanism which can simplify many things. However to use them properly requires much time and experience - in order to decide when their usage is appropriate.
For me the most important advantages are:
- reducing the repetition of code (generic containers, algorithms)
- reducing the repetition of code advanced (MPL and Fusion)
- static polymorphism (=performance) and other compile time calculations
- policy based design (flexibility, reusability, easier changes, etc)
- increasing safety at no cost (i.e. dimension analysis via Boost Units, static assertions, concept checks)
- functional programming (Phoenix), lazy evaluation, expression templates (we can create Domain-specific embedded languages in C++, we have great Proto library, we have Blitz++)
- other less spectacular tools and tricks used in everyday life:
- STL and the algorithms (what's the difference between
for
andfor_each
) - bind, lambda (or Phoenix) ( write clearer code, simplify things)
- Boost Function (makes writing callbacks easier)
- tuples (how to genericly hash a tuple? Use Fusion for example...)
- TBB (
parallel_for
and other STL like algorithms and containers)
- STL and the algorithms (what's the difference between
- Can you imagine C++ without templates? Yes I can, in the early times you couldn't use them because of compiler limitations.
- Would you write in C++ without templates? No, as I would lose many of the advantages mentioned above.
Downsides:
- Compilation time (for example throw in Sprit, Phoenix, MPL and some Fusion and you can go for a coffee)
- People who can use and understand templates are not that common (and these people are useful)
- People who think that they can use and understand templates are quite common (and these people are dangerous, as they can make a hell out of your code. However most of them after some education/mentoring will join the group mentioned in the previous point)
- template
export
support (lack of) - error messages could be less cryptic (after some learning you can find what you need, but still...)
I highly recommend the following books:
- C++ Templates: The Complete Guide by David Vandevoorde and Nicolai Josuttis (thorough introduction to the subject of templates)
- Modern C++ Design. Generic Programming and Design Patterns Applied by Andrei Alexandrescu (what is the less known way of using templates to simplify your code, make development easier and result in code robust to changes)
- C++ Template Metaprogramming by David Abrahms and Aleksey Gutov (again - different way of using the templates)
- More C++ Idioms from Wikibooks presents some nice ideas.
这篇关于C ++模板的好点和坏点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!