C# 泛型与 C++ 模板的比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1208153/
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
C# generics compared to C++ templates
提问by Bjarke Freund-Hansen
Possible Duplicate:
What are the differences between Generics in C# and Java… and Templates in C++?
What are the differences between C# generics compared to C++ templates? I understand that they do not solve exactly the same problem, so what are the pros and cons of both?
与 C++ 模板相比,C# 泛型之间有什么区别?我知道它们不能解决完全相同的问题,那么两者的优缺点是什么?
采纳答案by bdonlan
You can consider C++ templates to be an interpreted, functional programming language disguised as a generics system. If this doesn't scare you, it should :)
您可以将 C++ 模板视为一种伪装成泛型系统的解释型函数式编程语言。如果这不会吓到你,它应该:)
C# generics are very restricted; you can parameterize a class on a type or types, and use those types in methods. So, to take an example from MSDN, you could do:
C# 泛型非常受限制;您可以在一个或多个类型上参数化一个类,并在方法中使用这些类型。因此,以MSDN为例,您可以执行以下操作:
public class Stack<T>
{
T[] m_Items;
public void Push(T item)
{...}
public T Pop()
{...}
}
And now you can declare a Stack<int>
or Stack<SomeObject>
and it'll store objects of that type, safely (ie, no worried about putting SomeOtherObject
in by mistake).
现在您可以声明一个Stack<int>
orStack<SomeObject>
并且它会安全地存储该类型的对象(即,不必担心SomeOtherObject
错误放入)。
Internally, the .NET runtime will specialize it into variants for fundamental types like int, and a variant for object types. This allows the representation for Stack<byte>
to be much smaller than that of Stack<SomeObject>
, for example.
在内部,.NET 运行时会将其专门化为基本类型(如 int)的变体和对象类型的变体。例如,这允许 的表示Stack<byte>
远小于的表示Stack<SomeObject>
。
C++ templates allow a similar use:
C++ 模板允许类似的使用:
template<typename T>
class Stack
{
T *m_Items;
public void Push(const T &item)
{...}
public T Pop()
{...}
};
This looks similar at first glance, but there are a few important differences. First, instead of one variant for each fundamental type and one for all object types, there is one variant for each type it's instantiated against. That can be a lot of types!
乍一看,这看起来很相似,但有一些重要的区别。首先,不是每个基本类型都有一个变体,所有对象类型都有一个变体,而是针对它实例化的每种类型都有一个变体。那可以是很多类型!
The next major difference is (on most C++ compilers) it will be compiled in each translation unit it's used in. That can slow down compiles a lot.
下一个主要区别是(在大多数 C++ 编译器上)它将在使用它的每个翻译单元中编译。这会大大减慢编译速度。
Another interesting attribute to C++'s templates is they can by applied to things other than classes - and when they are, their arguments can be automatically detected. For example:
C++ 模板的另一个有趣的属性是它们可以应用于类以外的东西——当它们被应用时,它们的参数可以被自动检测到。例如:
template<typename T>
T min(const T &a, const T &b) {
return a > b ? b : a;
}
The type T will be automatically determined by the context the function is used in.
类型 T 将由函数使用的上下文自动确定。
These attributes can be used to good ends, at the expense of your sanity. Because a C++ template is recompiled for each type it's used against, and the implementation of a template is always available to the compiler, C++ can do very aggressive inlining on templates. Add to that the automatic detection of template values in functions, and you can make anonymous pseudo-functionsin C++, using boost::lambda. Thus, an expression like:
这些属性可以用于好的目的,但会牺牲你的理智。因为 C++ 模板会针对它所针对的每种类型重新编译,并且模板的实现始终可供编译器使用,所以 C++ 可以对模板进行非常积极的内联。再加上函数中模板值的自动检测,您可以使用boost::lambda在 C++ 中创建匿名伪函数。因此,表达式如下:
_1 + _2 + _3
_1 + _2 + _3
Produces an object with a seriously scarytype, which has an operator() which adds up its arguments.
产生一个非常可怕的类型的对象,它有一个 operator() 将其参数相加。
There are plenty of other dark corners of the C++ template system - it's an extremely powerful tool, but can be painful to think about, and sometimes hard to use - particularly when it gives you a twenty-page long error message. The C# system is much simpler - less powerful, but easier to understand and harder to abuse.
C++ 模板系统还有很多其他的黑暗角落——它是一个非常强大的工具,但考虑起来可能会很痛苦,有时很难使用——尤其是当它给你一个 20 页长的错误消息时。C# 系统要简单得多 - 功能较弱,但更容易理解且更难滥用。
回答by Paul Sonier
http://blogs.msdn.com/csharpfaq/archive/2004/03/12/88913.aspx
http://blogs.msdn.com/csharpfaq/archive/2004/03/12/88913.aspx
Roughly, much of the difference has to do with the fact that templates are resolved at compile-time, and generics are resolved at runtime.
粗略地说,大部分差异与模板在编译时解析而泛型在运行时解析的事实有关。
回答by Daniel Earwicker
This looks like a handy reference.
这看起来像一个方便的参考。
回答by Jeff Yates
This blog entry from Eric Gunnersoncovers this topic quite well.
Eric Gunnerson 的这篇博客文章很好地涵盖了这个主题。
The biggest immediate difference is that templates are a compile time feature whereas generics are a runtime feature.
最大的直接区别是模板是编译时功能,而泛型是运行时功能。
回答by Richard Berg
Extensive answer on Stack Overflow: What are the differences between Generics in C# and Java... and Templates in C++?
Stack Overflow 上的广泛回答:C# 和 Java 中的泛型与 C++ 中的模板有什么区别?