什么是C ++模板和Java / C#泛型,哪些是限制的差异? [英] What are the Differences between C++ Templates and Java/C# Generics and what are the limits?

查看:213
本文介绍了什么是C ++模板和Java / C#泛型,哪些是限制的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到一篇有趣的文章/主题/从这里讨论我得到了以下几个问题:




  • 什么是Java / C#泛型的局限性

  • 什么是可能的? C ++模板那是不可能的与Java / C#泛型?



修改1 更多推荐的埃里克利珀




  • 有哪些图案,可以用C#泛型,但C ++模板不可能的?

  • 什么是C#的真正的泛型类型和Java的类型擦除泛型类型?


  • 解决方案

    首先,你可能需要阅读的my文章对这个问题



    的主要区别在我的脑海C ++模板和C#泛型之间是C ++模板实际上的完全在构造模板重新编译代码的。优点和C ++的方法的缺点是多方面的:




    • PRO:您可以有效地创建一个像类型参数T必须限制有一个加法运算符;如果代码中加入对方几个TS的话,如果你用一种论点,即不允许另建​​有它的模板将无法编译。


    • CON:您可以意外地创建无证的限制,如类型参数T必须有一个加法运算符




    在你有C#的的什么约束,这有助于用户,但仅限于一小部分可能的约束:接口,基类,价值VS引用类型和默认构造函数的约束,这是。所有的




    • PRO:语义分析,可以为两个不同的结构完全不同。如果你想要的,这是真棒


    • CON:语义分析,可以为两个不同的结构完全不同。如果你不希望出现这种情况,这是一个等待发生的错误。




    在C#中的语义分析完成一旦EM>无论多少次的类型构造任何的类型满足的约束,而不仅仅是实际提供的类型参数的参数。




    • PRO:你只生成正是你需要的构造代码


    • CON:你为所有使用结构的代码。




    模板可引起CODEGEN得到很大。在C#中,IL为泛型类型生成一次,然后在运行时的抖动并CODEGEN为您的程序使用的所有类型。这有一个小的性能开销,但它是由一个事实,即抖动实际上只用于生成代码一次有所减轻的所有引用类型变量的。所以,如果你有列表<对象> 列表<串> 然后即时编译代码只产生一次,并用于都。 列表< INT> 列表<短方式> 相比之下即时编译器代码两次




    • PRO:当你使用一个模板库,你的源代码就在那里


    • CON:使用您的模板库中的源代码。




    在C#中,泛型类型都是一流的类型。如果你在图书馆坚持它们,可以使用该库的任何地方,而无需运送的源代码



    最后:




    • PRO:模板许可证模板元编程


    • CON:模板元编程是很难理解为新手


    • CON:模板系统实际上是不允许某些类型的拓扑结构是一个通用系统非常简单




    例如,我想,这将是很难做这样的事情在C ++:

      D类< T> 
    {
    S类{}
    D< D​​< T>&.S GT; DS;
    }

    在C#泛型,没有问题。在运行时,类型仅内置的一次的所有引用类型的参数。



    但在C ++模板,当你有<$ C $发生了什么C> D< INT> ?内部构造类型类型的字段 D< D​​< INT>&.S GT; ,所以我们需要构建一个类型。但是,这种类型的构造型 D<的领域; D< D​​< INT> .S> .S> ...等,以无限


    I read an interesting Article/Thread/Discussion from here and i got following questions:

    • What are the limitations of Java/C# generics?
    • What is possible with C++ Templates that is impossible with Java/C# generics?

    Edit 1 More recommended questions by Eric Lippert

    • What are some patterns that are possible with C# generics but impossible with C++ templates?
    • What's the difference between C#'s true generic types and Java's type erasure generic types?

    解决方案

    First off, you might want to read my 2009 article on this subject.

    The primary difference to my mind between C++ templates and C# generics is that C++ templates actually completely recompile the code upon construction of the template. The pros and cons of the C++ approach are many:

    • PRO: You can effectively create constraints like "the type argument T must have an addition operator"; if the code contains a couple of Ts added to each other then the template will not compile if you construct it with a type argument that doesn't permit addition.

    • CON: You can accidentally create undocumented constraints like "the type argument T must have an addition operator".

    In C# you have to say what the constraints are which helps the user, but you are limited to only a small set of possible constraints: interfaces, base classes, value vs reference type and default constructor constraints, and that's all.

    • PRO: Semantic analysis can be completely different for two different constructions. If you want that, that's awesome.

    • CON: Semantic analysis can be completely different for two different constructions. If you don't want that, that's a bug waiting to happen.

    In C# the semantic analysis is done once no matter how many times the type is constructed, and it is therefore required to work with any type argument that meets the constraints, not just the type arguments that are actually supplied.

    • PRO: You only generate the code for exactly the constructions you need.

    • CON: You generate the code for all the constructions you use.

    Templates can cause codegen to get large. In C#, the IL for a generic type is generated once, and then at runtime the jitter does codegen for all the types your program uses. This has a small performance cost, but it is mitigated somewhat by the fact that the jitter actually only generates code once for all reference type arguments. So if you have List<object> and List<string> then the jitted code is only generated once and used for both. List<int> and List<short> by contrast jits the code twice.

    • PRO: when you use a template library, you have the source code right there.

    • CON: to use a template library you have to have the source code.

    In C#, generic types are first-class types. If you stick them in a library, you can use that library anywhere without having to ship the source code.

    And finally:

    • PRO: Templates permit template metaprogramming.

    • CON: Template metaprogramming is hard to understand for novices.

    • CON: The template system actually does not permit some type topologies that are extremely straightforward in a generic system.

    For example, I imagine that it would be difficult to do something like this in C++:

    class D<T> 
    {
        class S { }
        D<D<T>.S> ds;
    }
    

    In C# generics, no problem. At runtime the type is only built once for all reference type arguments.

    But in C++ templates, what happens when you have D<int>? The interior type constructs a field of type D<D<int>.S>, so we need to construct that type. But that type constructs a field of type D<D<D<int>.S>.S>... and so on to infinity.

    这篇关于什么是C ++模板和Java / C#泛型,哪些是限制的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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