有没有强大的理由不使用模板? [英] Is there any strong reason for not using Templates?

查看:66
本文介绍了有没有强大的理由不使用模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我只是想知道是否有任何强大的理由不使用模板

。当我们使用模板时,它将为不同的数据类型复制

代码,从而增加可执行文件的大小。

这不会导致任何性能问题。


所以,如果我可以提供更大的图像尺寸我是否可以安全使用
可以继续使用模板而不用担心任何其他的

问题?


谢谢。

Hi,

I am just trying to find out if there is any strong reason for not
using Templates. When we use Templates it is going to replicate the
code for different data types, thus increasing the executable size.
This should not cause any performance issue.

So, is it safe to say that if I can offered to have bigger image size I
can go ahead and use Templates with out worrying about any other
issues?

Thanks.

推荐答案

NewToCPP写道:
NewToCPP wrote :

我只是想知道是否有任何强有力的理由不使用模板


I am just trying to find out if there is any strong reason for not
using Templates.



没有。

There is none.


所以说,如果我是安全的话可以提供更大的图像尺寸我

可以继续使用模板,而不用担心任何其他

问题?
So, is it safe to say that if I can offered to have bigger image size I
can go ahead and use Templates with out worrying about any other
issues?



它不会更大。

因为如果你想要实现相同的功能而不需要

模板你必须为你需要的每种类型写一个函数,

会产生相同的结果。

It won''t be bigger.
Because if you wanted to achieve the same functionality without
templates you would have to write a function for each type you need,
which would have the same result.


以下是我从维基百科得到的

优点和缺点

========================= =

模板的某些用途,例如max()函数,以前是由类似函数的预处理器宏填充的(这是C的遗留问题) b $ b编程语言)。例如,这是一个可能的max()宏:


#define max(a,b)((a)<(b)?(b):( a))


在编译时扩展宏和模板。宏是

总是内联扩展;当编译器认为合适时,模板也可以扩展为内联

函数。因此,两个类似函数的宏和函数模板都没有运行时开销。


但是,模板通常被认为是对宏的改进

用于这些目的。模板是类型安全的。模板避免了一些代码中发现的常见错误,这些错误大量使用类似函数的

宏。也许最重要的是,模板被设计为适用于比宏更大的问题。


使用模板有三个主要缺点:编译器

支持,错误消息和代码膨胀。许多编译器

历史上对模板的支持非常差,因此使用

模板会使代码的可移植性降低。当C ++编译器与链接器一起使用时,支持也可能是差的,而不是支持C ++的,或者当尝试在共享库中使用模板时>
边界。现在大多数现代编译器都具有相当强大且支持b $ b的标准模板支持。


几乎所有编译器都会产生混乱,冗长或有时无用的功能
$ b在使用模板的代码中检测到错误时出现$ b错误消息。

这可能使模板难以开发。


最后,使用模板可能导致编译器生成额外的代码(模板的实例化),所以不加区别地使用模板会导致代码膨胀,导致代码过大

可执行文件。但是,在某些情况下,明智地使用模板专业化可以大大减少代码膨胀。模板生成的额外

实例化也可能导致调试器难以使用模板优雅地工作
。例如,在源文件中在模板中设置

调试断点可能会错过

在实际实例化中设置断点或者可能设置一个

断点在模板实例化的每个地方。

The following is what I got from Wikipedia
Advantages and disadvantages
==========================
Some uses of templates, such as the max() function, were previously
filled by function-like preprocessor macros (a legacy of the C
programming language). For example, here is a possible max() macro:

#define max(a,b) ((a) < (b) ? (b) : (a))

Both macros and templates are expanded at compile time. Macros are
always expanded inline; templates can also be expanded as inline
functions when the compiler deems it appropriate. Thus both
function-like macros and function templates have no run-time overhead.

However, templates are generally considered an improvement over macros
for these purposes. Templates are type-safe. Templates avoid some of
the common errors found in code that makes heavy use of function-like
macros. Perhaps most importantly, templates were designed to be
applicable to much larger problems than macros.

There are three primary drawbacks to the use of templates: compiler
support, poor error messages, and code bloat. Many compilers
historically have very poor support for templates, so the use of
templates can make code somewhat less portable. Support may also be
poor when a C++ compiler is being used with a linker which is not
C++-aware, or when attempting to use templates across shared library
boundaries. Most modern compilers though now have fairly robust and
standard template support.

Almost all compilers produce confusing, long, or sometimes unhelpful
error messages when errors are detected in code that uses templates.
This can make templates difficult to develop.

Finally, the use of a templates may cause the compiler to generate
extra code (an instantiation of the template), so the indiscriminate
use of templates can lead to code bloat, resulting in excessively large
executables. However, judicious use of template specialization can
dramatically reduce such code bloat in some cases. The extra
instantiations generated by templates can also cause debuggers to have
difficulty working gracefully with templates. For example, setting a
debug breakpoint within a template from a source file may either miss
setting the breakpoint in the actual instantiation desired or may set a
breakpoint in every place the template is instantiated.


为什么


几乎所有的编译器在使用模板的代码中检测到错误时,会产生令人困惑,长时间或有时无用的错误消息。

Why does

"Almost all compilers produce confusing, long, or sometimes unhelpful
error messages when errors are detected in code that uses templates."


这篇关于有没有强大的理由不使用模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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