C#泛型VS C ++模板 - 需要澄清的限制 [英] C# generics vs C++ templates - need a clarification about constraints

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

问题描述

什么是C#和Java泛型之间的区别...在C ++?


大家好

我经历的C ++程序员,但很新的C#。

I am experienced C++ programmer but quite new to C#.

怎么了这些约束和仿制药?为什么它不工作一样在C ++中,其中的约束是隐式的和你做模板类的实例化出来的?

Whats up with those constraints and generics? Why doesn't it work the same as in C++ where constraints are implicit and derived from the instantiations you do to the template class?

为什么不是微软让它相同的方式工作在C ++?

Why didn't Microsoft make it work the same way as in C++?

推荐答案

那么,在一般情况下,C ++模板和C#泛型的类似的 - 相比于Java泛型它们的完全的不同,但他们也较大差异。像在C#中,有通过使用反射,获得描述用于实例化一个泛型类型的对象运行时支持。 C ++没有反思,以及所有它与类型是在编译时完成。

Well, in general, C++ templates and C# generics are similar - compared to Java generics which are completely different, but they have also large differences. Like in C#, there is runtime support by using reflection, getting an object describing the types used to instantiate a generics. C++ doesn't have reflection, and all it does with types is done at compile time.

C#泛型和C ++模板之间最大的区别确实是C#泛型是更好的类型检查。他们总是有限的,在某种意义上说,他们不允许未在规定仿制药的时间规定有效运作。提出的一个原因那增加的复杂性,将采取具有隐含的限制C#的首席设计师。我不能很好地用C#熟悉,所以我不能在这里再说话。我将讨论有关事项是如何在C ++以及它们是如何将得到改善,使人们不认为C ++的东西是完全错误的。

The biggest difference between C# generics and C++ templates indeed are that C# generics are better type checked. They are always constrained, in the sense that they don't allow operations that are not stated valid at the time of defining the generics. C#'s chief designer raised as a reason of that the added complexity it would have taken to have implied constraints. I'm not well versed with C#, so i can't talk further here. I'll talk about about how matters are in C++ and how they are going to be improved, so that people don't think C++'s stuff is all wrong.

在C ++中,模板不限制。如果你做一个操作,在模板定义时它被暗示该操作将在实例化时取得成功。它甚至没有要求的模板语法检查的有效性C ++编译器。如果它包含一个语法错误,那么这个错误已被诊断为实例。在此之前的任何诊断是实现一个纯粹的伪善。

In C++, templates are not constrained. If you do an operation, at template definition time it is implied that the operation will succeed at instantiation time. It's not even required to a C++ compiler that the template is syntactically checked for validity. If it contains a syntax error, then that error has to be diagnosed at instantiation. Any diagnose before that is a pure goody of the implementation.

这些隐含的约束已经证明是很容易在短期内模板设计者,因为他们不必关心自己的模板接口声明的有效操作。他们把负担其模板的用户 - 这样的用户,以确保他满足所有这些要求。通常,它发生在用户尝试看似合法的操作,但失败了,与编译器给用户数以百计的错误消息的行一些无效语法或者找不到名称。因为编译器不知道的什么的特别约束被摆在首位侵犯,它列出了和有史以来参与各地发生故障代码路径,所有部件全部都不重要细节,用户将有通过可怕的错误消息文本抓​​取。

Those implied constraint have shown to be easy for the template designer in the short term, because they don't have to care about stating the valid operations in their template interface. They put the burden on the user of their template - so the user has to make sure he fulfills all those requirements. Often it happens that the user tries seemingly valid operations but fails, with the compiler giving the user hundreds of lines of error messages about some invalid syntax or not found names. Because the compiler can't know what constraint in particular was violated in the first place, it lists all parts of code paths ever involved around the faulty place and all not even important details, and the user will have to crawl through the horrible error message text.

这是一个根本性的问题,可以通过在界面上只是陈述了一个模板或泛型什么样的属性类型参数来解决必须有。 C#,据我所知,能约束来实现一个接口或者继承一个基类的参数。它解决了一个类型的水平。

That is a fundamental problem, which can be solved by just stating at the interface for a template or generics what properties a type parameter has to have. C#, as far as i know it, can constraint the parameter to implement an interface or inherit a base-class. It solves that on a type-level.

在C ++委员会一直认为有必要解决这些问题,<击>很快(明年可能),C ++将有办法说出如此明确限制太请参阅下面的时间机器笔记),如以下情形。

The C++ committee has long seen there is need to fix these problems, and soon (next year, probably), C++ will have a way to state such explicit constraints too (see time-machine note below), as in the following case.

template<typename T> requires VariableType<T>
T f(T a, T b) {
    return a + b; 
}



编译器信号在这一点上一个错误,因为书面表达的是不打上了要求有效。这首先有助于模板的设计者写更多的正确的代码的,因为代码已经进行类型检查在一定程度上(井什么是可能的存在)。程序员现在可以声明,要求:

The compiler signals an error at that point, because the expression as written is not marked valid by the requirements. This first helps the designer of the template to write more correct code, because the code is type-checked already to some degree (well to what is possible there). The programmer can now state that requirement:

template<typename T> requires VariableType<T> && HasPlus<T, T>
T f(T a, T b) {
    return a + b; 
}

现在,它将编译器。编译器,通过看 T 显示为返回类型,自动暗示 T 是拷贝,但因为使用 T 出现在界面中,而不是在模板身上。其他的要求是使用要求的条款规定。现在,如果他使用一种类型,不具有+ 定义的 OP用户将得到一个相应的错误信息。

Now, it will compiler. The compiler, by seeing T appearing as the return type, automatically implied that T is copyable, because that use of T appears in the interface, rather than in the templates body. The other requirements were stated using requirement clauses. Now, the user will get a appropriate error message if he uses a type that doesn't have an op+ defined.

C ++ 1X解耦的类型的要求。基本类型的上述工程藏汉作为类。在这个意义上,他们更灵活,但颇有几分复杂。这种状态时,当要求得到满足的规则是长...您可以用新的规则说以下内容:

C++1x decouples the requirements from the type. The above works for primitive types aswell as for classes. In this sense, they are more flexible, but quite a bit complex. The rules that state when and when requirements are satisfied are long... You can with the new rules say the following:

template<typename T> requires MyCuteType<T>
void f(T t) { *t = 10; }

然后,调用˚F INT !这将仅通过写了 MyCuteType℃的概念图工作; INT> ,教编译一个int如何取消引用。它会在这样的循环,会非常方便:

And then, call f with an int! That would work by just writing a concept map for MyCuteType<int> that teaches the compiler how an int can be dereferenced. It will get quite handy in loops like this:

for_each(0, 100, doSomething());



由于程序员可以告诉编译器一个int如何满足一个输入迭代器,你实际上可以在C ++ 1x写这样的代码,如果你只写相应的概念图,这真的不是那么困难。

Since the programmer can tell the compiler how an int can satisfy the concept of an input iterator, you could actually write such code in C++1x, if you only write the appropriate concept map, which really isn't all that difficult.

好了,够了与此有关。我希望我能告诉你,有约束的模板是不是所有的坏,但实际上的更好的,因为关系中间人类型和模板中对他们的操作现在是由编译器闻名。而且我还没有写过关于公理,这是在的另一个好处C ++ 1X '的概念。记住的这是未来的东西的,这还没有出来,但它会大约在2010年。然后,我们将不得不等待一些编译器来实现,所有的:)

Ok, enough with this. I hope i could show you that having templates constrained is not all that bad, but in fact better, because the relationship betweens types and the operations on them within the templates are now known by the compiler. And i haven't even written about axioms, which are another nice thing in C++1x' concepts. Remember that this is future stuff, it's not yet out, but it will approximately at 2010. Then we will have to wait for some compiler to implement that all :)

C ++ 0x的概念是的接受成稿,但在后期的2009年太糟糕了,已经投了出去!但是,也许我们会在未来的C ++版本再次看到了吗?让我们所有的希望!

C++0x concepts were not accepted into the draft but have been voted out at late of 2009. Too bad! But perhaps we will see it again in the next C++ version? Let's all hope!

这篇关于C#泛型VS C ++模板 - 需要澄清的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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