C# 泛型与 C++ 模板的比较 [英] C# generics compared to C++ templates

查看:29
本文介绍了C# 泛型与 C++ 模板的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
有什么区别在 C# 和 Java 中的泛型之间…和 C++ 中的模板?

与 C++ 模板相比,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++ 模板视为一种伪装成泛型系统的解释型函数式编程语言.如果这没有吓到你,它应该:)

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# 泛型非常受限制;您可以在一个或多个类型上参数化一个类,并在方法中使用这些类型.因此,以 MSDN 中的示例为例,您可以这样做:

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:

public class Stack<T>
{
   T[] m_Items; 
   public void Push(T item)
   {...}
   public T Pop()
   {...}
}

现在你可以声明一个 StackStack 并且它会安全地存储那种类型的对象(即,不用担心把SomeOtherObject 错误输入).

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).

在内部,.NET 运行时会将其专门化为基本类型(如 int)的变体和对象类型的变体.例如,这允许 Stack 的表示比 Stack 的表示小得多.

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.

C++ 模板允许类似的使用:

C++ templates allow a similar use:

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!

下一个主要区别是(在大多数 C++ 编译器上)它将在使用它的每个翻译单元中编译.这会大大减慢编译速度.

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:

template<typename T>
T min(const T &a, const T &b) {
  return a > b ? b : a;
}

类型 T 将根据函数使用的上下文自动确定.

The type T will be automatically determined by the context the function is used in.

这些属性可以用于好的目的,但会牺牲您的理智.因为 C++ 模板会针对它所针对的每种类型重新编译,并且模板的实现始终可供编译器使用,所以 C++ 可以对模板进行非常积极的内联.添加到函数中模板值的自动检测,您可以使 C++ 中的匿名伪函数,使用 boost::拉姆达.因此,表达式如下:

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-functions in C++, using boost::lambda. Thus, an expression like:

_1 + _2 + _3

_1 + _2 + _3

产生一个非常可怕类型的对象,它有一个 operator() 来将其参数相加.

Produces an object with a seriously scary type, which has an operator() which adds up its arguments.

C++ 模板系统还有很多其他的黑暗角落——它是一个非常强大的工具,但考虑起来可能会很痛苦,有时很难使用——尤其是当它给你一个 20 页长的错误消息时.C# 系统要简单得多 - 功能不那么强大,但更容易理解且更难滥用.

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# 泛型与 C++ 模板的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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