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

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

问题描述


可能重复:

- > - > - >和模板在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()
   {...}
}

现在你可以声明一个 Stack< int> Stack< SomeObject> ,它会安全地存储该类型的对象(即,不必担心将 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< byte> 的表示比 Stack< SomeObject> 的表示小得多。

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 ++可以对模板执行非常激进的内联。添加到函数中自动检测模板值,您可以使匿名伪函数,使用 boost :: lambda 。因此,表达式如下:

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

C ++模板系统中有许多其他黑暗的角落 - 它是一个非常强大的工具,但是可以痛苦地考虑,有时很难使用 - 特别是当它给你一个二十页长的错误消息。 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天全站免登陆