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

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

问题描述

可能重复:

什么是C#和Java&hellip仿制药之间的差别;并用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()
   {...}
}

和现在你可以声明堆栈< INT> 堆栈< 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和对象类型的变种变种。这允许表示堆栈<字节> 来比堆叠式和LT小得多; 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 ++可以做模板非常积极的内联。再加上函数模板值的自动检测,你可以做的匿名伪的功能在C ++中,使用提升: :拉姆达。因此,像一个表达式:

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

产生一个与的对象认真。吓人的类型,它有一个操作符(),这增加了它的参数

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

有大量的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天全站免登陆