C ++模板的好点和坏点是什么? [英] What are the good and bad points of C++ templates?

查看:106
本文介绍了C ++模板的好点和坏点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在和朋友交谈,有些人完全同意应该使用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类似算法和容器)


  • 你能想象C ++没有模板吗?是的,我可以,在早期,你不能使用它们,因为编译器的限制。

  • 你会用C ++编写没有模板?不,我会失去上述许多优点。



缺点:




  • 编译时间(例如投掷Sprit,Phoenix,MPL和一些Fusion,您可以去喝咖啡)

  • 使用和了解模板并不常见(这些人很有用)

  • 人们认为他们可以使用和理解模板是很常见的(这些人是危险的,因为他们可以让你的代码,但他们大部分后一些教育/指导将加入上述提到的小组)

  • template export 支持(缺少)

  • 错误消息可能不那么神秘(在学习之后,您可以找到所需的内容, 。)



我强烈推荐以下书籍:




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 and for_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)
  • 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 ++模板的好点和坏点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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