使用模板有什么缺点?

发布于 2024-08-31 08:48:33 字数 63 浏览 12 评论 0原文

一些缺点是

  1. 它的语法很复杂,
  2. 编译器会生成额外的代码

Some of the disadvantages would be

  1. its syntax is complex
  2. compiler generates extra code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

唔猫 2024-09-07 08:48:34

更少的人理解它们,特别是在元编程层面,因此更少的人可以维护它们。

Less people understand them, epsecially at the level of meta programming, therefore less people can maintain them.

时间你老了 2024-09-07 08:48:34

当您使用模板时,编译器仅生成您实际使用的内容。我认为使用 C++ 模板元编程没有任何缺点,除了编译时间可能会很长,如果您使用像 boost 或 loki 库那样非常复杂的结构,编译时间可能会很长。

When you use templates, your compiler only generates what you actually use. I don't think there is any disadvantages in using C++ template meta-programming except the compiling time which can be quite long if you used very complex structures as boost or loki libraries do.

莫相离 2024-09-07 08:48:34

缺点:模板错误仅在实例化模板时由编译器检测到。有时,只有在实例化成员方法时才会检测到模板方法中的错误,而不管模板的其余部分是否已实例化。

如果模板类的方法中存在错误,仅引用一个函数,但其​​他代码使用该模板而不使用该方法,则编译器将不会生成错误,直到错误的方法被实例化。

A disadvantage: template errors are only detected by the compiler when the template is instantiated. Sometimes, errors in the methods of templates are only detected when the member method is instantiated, regardless if the rest of the template is instantiated.

If I have an error in a method, of a template class, that only one function references, but other code uses the template without that method, the compiler will not generate an error until the erroneous method is instantiated.

2024-09-07 08:48:34

最糟糕的情况是:您从错误的模板代码中收到编译器错误消息。

The absolute worst: The compiler error messages you get from bad template code.

许仙没带伞 2024-09-07 08:48:34

多年来我有时会使用模板。它们可能很方便,但从专业角度来看,我倾向于远离它们。其中两个原因是:

1.

需要 a.) 将函数定义(不仅仅是声明)“源”代码暴露给“使用位置”代码,或者 b.) 在源文件中创建虚拟实例化。这是编译所需要的。选项 a.) 可以通过在标头中定义函数或实际包含 cpp 来完成。

我们容忍 C++ 中的标头(例如与 C# 相比)的原因之一是“接口”与“实现”的分离。嗯,模板似乎与这一理念不一致。

2.

模板类型参数实例化所调用的函数可能不会在编译时强制执行,从而导致链接错误。例如 T 示例; example.CompilerDoesntKnowIfThisFunctionExistsOnT();
恕我直言,这是“松散的”。

解决方案:

我倾向于使用基类,而不是模板,派生类/容器类知道在编译时可用的内容。基类可以提供模板经常使用的通用方法和“类型”。这就是为什么如果需要修改现有代码以在需要的继承层次结构中插入通用基类,源代码可用性会很有帮助。否则,如果代码是闭源的,请使用通用基类更好地重写它,而不是使用模板作为解决方法。

如果类型不重要,例如向量< T>那么只使用“对象”怎么样? C++ 没有提供“object”关键字,我向 Bjarne Stroustrup 博士建议,这将很有帮助,尤其是告诉编译器和阅读代码的人类型并不重要(对于不重要的情况)。我不认为 C++11 有这个,也许 C++14 会有?

I have used templates sometimes over the years. They can be handy but from a professional perspective, I am leaning away from them. Two of the reasons are:

1.

The need to either a.) expose the function definitions (not only declarations) "source" code to the "where used" code or b.) create a dummy instantiation in the source file. This is needed for compilation. Option a.) can be done by defining functions in the header or actually including the cpp.

One of the reasons that we tolerate headers in C++ (compared to C# for example) is because of the separation of "interface" from "implementation". Well, templates seem to be inconsistent with this philosophy.

2.

Functions called by a template type parameter instantiation may not be enforced at compile time resulting in link errors. E.g. T example; example.CompilerDoesntKnowIfThisFunctionExistsOnT();
This is "loose" IMHO.

Solutions:

Rather then templates, I lean towards using a base class whereby the derived/container classes know what is available at compile time. The base classes can provide the generic methods and "types" that templates are often used for. This is why source code availability can be helpful if existing code needs to be modified to insert a generic base class in the inheritance hierarchy where needed. Otherwise if, code is closed source, rewrite it better using generic base classes instead of using a template as a work around.

If type is unimportant e.g. vector< T > then how about just using"object". C++ has not provided an "object" keyword and I have proposed to Dr. Bjarne Stroustrup that this would be helpful especially to tell compiler and people reading code that type is not important (for cases when it isn't). I don't that think C++11 has this, perhaps C++14 will?

奢欲 2024-09-07 08:48:33

它们很难被验证。不被使用的模板代码往往很少被编译。因此,良好的测试用例覆盖率是必须的。但测试非常耗时,然后可能会发现代码从一开始就不需要健壮。

They are hard to validate. Template code which doesn't get used tends to be seldom compiled at all. Therefore good coverage of test cases is a must. But testing is time-consuming, and then it may turn out the code never needed to be robust in the first place.

故事↓在人 2024-09-07 08:48:33

嗯,怎么样...

3:它们的编译速度可能很慢

4:它们强制在编译时而不是运行时计算东西(如果您更喜欢快速执行速度而不是运行时灵活性,这也可能是一个优势)

5:较旧的 C++ 编译器不处理它们,或者不能正确处理它们

6:当您没有正确获取代码时,它们生成的错误消息可能几乎难以理解

Hmm, how about...

3: They can be slow to compile

4: They force things to be calculated at compile time rather than run time (this can also be an advantage, if you prefer fast execution speed over runtime flexibility)

5: Older C++ compilers don't handle them, or don't handle them correctly

6: The error messages that they generate when you don't get the code right can be nearly incomprehensible

二货你真萌 2024-09-07 08:48:33

模板将您的实现公开给代码的客户端,如果您在库边界传递模板化对象,那么维护 ABI 会变得更加困难。

Templates expose your implementation to the clients of your code, which makes maintaining your ABI harder if you pass templated objects at library boundaries.

自此以后,行同陌路 2024-09-07 08:48:33

唯一真正的缺点是,如果您在模板中犯了任何微小的语法错误(尤其是其他模板使用的错误),则错误消息不会会有帮助......预计有几页几乎无法使用错误消息;-)。编译器的缺陷是特定于编译器的,并且语法虽然丑陋,但并不是真正“复杂”。总而言之,尽管正确的错误诊断存在巨大问题,但模板仍然是 C++ 中唯一最好的东西,它很可能会诱使您使用 C++ 而不是其他语言的劣质语言。泛型的实现,例如 Java...

The only real disadvantage is that if you make any tiny syntax error in a template (especially one used by other templates) the error messages are not gonna be helpful... expect a couple pages of almost-unusable error msgs;-). Compilers' defect are very compiler-specific, and the syntax, while ugly, is not really "complex". All in all, though -- despite the huge issue with proper error diagnostics -- templates are still the single best thing about C++, the one thing that might well tempt you to use C++ over other languages with inferior implementations of generics, such as Java...

峩卟喜欢 2024-09-07 08:48:33

到目前为止,似乎没有人提到我发现模板的主要缺点:代码可读性直线下降!

我不是指语法问题——是的,语法很丑陋,但我可以原谅这一点。我的意思是这样的:我发现,对于以前从未见过的非模板化代码,无论应用程序有多大,如果我从 main() 开始,我通常可以解码一个大体的内容:程序运行没有问题。仅仅使用 vector 或类似的代码丝毫不会困扰我。但是,一旦代码开始定义和使用自己的模板来实现简单容器类型之外的目的,可理解性很快就会消失。 这对代码维护具有非常负面的影响。

其中一部分是不可避免的:模板通过复杂的偏序重载解析规则(对于函数模板)提供了更大的表达能力,并且在较小程度上,部分专业化(对于类模板)。但这些规则实在是太复杂了,甚至编译器编写者(我很高兴地承认他们比我聪明一个数量级)在极端情况下仍然会出错。

C++ 中命名空间、友元、继承、重载、自动转换和参数相关查找的交互已经足够复杂了。但是,当您将模板添加到组合中,以及对它们附带的名称查找和自动转换规则进行细微更改时,复杂性可能会达到人类无法处理的程度。我只是不相信自己能够阅读和理解使用所有这些构造的代码。


与模板无关的一个困难是调试器仍然难以自然地显示 STL 容器的内容(与 C 样式数组相比)。

So far no-one seems to have mentioned the main disadvantage I find with templates: code readability plummets!

I'm not referring to syntax issues -- yes the syntax is ugly, but I can forgive that. What I mean is this: I find that with never-seen-before non-templated code, however large the application is, if I start at main() I can usually decode the broad strokes of what a program is doing without problems. And code that merely uses vector<int> or similar doesn't bother me in the slightest. But once code starts to define and use its own templates for purposes beyond simple container types, understandability rapidly goes out the window. And that has very negative implications for code maintenance.

Part of that is unavoidable: templates afford greater expressiveness via the complicated partial-order overload resolution rules (for function templates) and, to a lesser degree, partial specialisation (for class templates). But the rules are so damn complicated that even compiler writers (who I'm happy to acknowledge as being an order of magnitude smarter than I am) are still getting them wrong in corner cases.

The interaction of namespaces, friends, inheritance, overloading, automatic conversions and argument-dependent lookup in C++ is already complicated enough. But when you add templates into the mix, as well as the slight changes to rules for name lookup and automatic conversions that they come with, the complexity can reach proportions that, I would argue, no human can deal with. I just don't trust myself to read and understand code that makes use of all these constructs.


An unrelated difficulty with templates is that debuggers still have difficulty showing the contents of STL containers naturally (as compared to, say, C-style arrays).

你穿错了嫁妆 2024-09-07 08:48:33

它们对于编译器来说解析起来很复杂,这意味着编译时间会增加。此外,如果您具有高级模板构造,则可能很难解析编译器错误消息。

They're complicated for the compiler to parse which means your compilation time will increase. Also it can be hard to parse compiler error messages if you have advanced template constructions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文