什么是Java等价的C ++模板?

时间:2021-06-07 21:00:53

What is the Java equivalent of C++'s templates?

什么是Java等价的C ++模板?

I know that there is an interface called Template. Is that related?

我知道有一个名为Template的界面。这有关系吗?

6 个解决方案

#1


45  

Templates as in C++ do not exist in Java. The best approximation is generics.

Java中不存在C ++中的模板。最好的近似是泛型。

One huge difference is that in C++ this is legal:

一个巨大的区别是在C ++中这是合法的:

<typename T> T sum(T a, T b) { return a + b; } 

There is no equivalent construct in Java. The best that you can say is

Java中没有等效的构造。你能说的最好的是

<T extends Something> T Sum(T a, T b) { return a.add(b); }

where Something has a method called add.

Something有一个名为add的方法。

In C++, what happens is that the compiler creates a compiled version of the template for all instances of the template used in code. Thus if we have

在C ++中,会发生的是编译器为代码中使用的模板的所有实例创建模板的编译版本。因此,如果我们有

int intResult = sum(5, 4);
double doubleResult = sum(5.0, 4.0);

then the C++ compiler will compile a version of sum for int and a version of sum for double.

那么C ++编译器将为int编译sum的版本,为double编译sum的版本。

In Java, there is the concept of erasure. What happens is that the compiler removes all references to the generic type parameters. The compiler creates only one compiled version of the code regardless of how many times it is used with different type parameters.

在Java中,存在擦除的概念。会发生什么是编译器删除对泛型类型参数的所有引用。编译器只创建一个编译版本的代码,无论它与不同类型参数一起使用多少次。

Other differences

  • C++ does not allow bounding of type parameters whereas Java does
  • C ++不允许绑定类型参数,而Java则允许

  • C++ allows type parameters to be primitives whereas Java does not
  • C ++允许类型参数为原语,而Java则不允许

  • C++ allows templates type parameters to have defaults where Java does not
  • C ++允许模板类型参数具有Java不具有的默认值

  • C++ allows template specialization whereas Java does not And, as should be expected by this point, C++ style template metaprogramming is impossible with Java generics.
  • C ++允许模板专业化,而Java则不然而且,正如应该预期的那样,Java泛型不可能使用C ++样式模板元编程。

  • Forget about seeing the curiously recurring template pattern in Java
  • 忘记在Java中看到奇怪的重复模板模式

  • Policy-based design is impossible in Java
  • 基于策略的设计在Java中是不可能的

#2


5  

There are no real templates in Java. C++ templates are compile-time entities that are used to generate classes. At runtime, there is no trace of them.

Java中没有真正的模板。 C ++模板是用于生成类的编译时实体。在运行时,没有它们的痕迹。

In Java, there are parameterized types as part of a mechanism called generics. It serves a similar purpose, but is significantly different in how it operates and its implications. It has some representation at runtime, there are specific rules, etc.

在Java中,参数化类型是称为泛型的机制的一部分。它有类似的用途,但在操作方式和含义方面有很大差异。它在运行时有一些表示,有特定的规则等。

Start by reading the Java tutorial, then read Bloch's Effective Java for a detailed description of the caveats if you want to be a "power user".

首先阅读Java教程,然后阅读Bloch的Effective Java,了解如果您想成为“超级用户”的详细说明。

#3


2  

There are no templates in Java. The only thing that is comparable with templates are Java Generics.

Java中没有模板。唯一可与模板相媲美的是Java Generics。

http://java.sun.com/developer/technicalArticles/J2SE/generics/

#4


0  

Java has generics which are similar but not exactly the same as templates. I don't know what the Template interface is but it has nothing to do with C++ templates.

Java具有类似但与模板不完全相同的泛型。我不知道Template接口是什么,但它与C ++模板无关。

#5


0  

There is no build in template mechanism in java. In stead they have generics. Moreover, ides have something called code templates e.g. for eclipse.

java中没有构建模板机制。相反,他们有泛型。此外,ides有一些称为代码模板的东西,例如为了日食。

#6


0  

You don't really need templates in Java. Templates are mainly useful when you need to have completely different types as a parameter that you know nothing about. In Java, all objects are virtual and inherit from one root object, and primitive types are defined much more clearly and casts work in a more sensical manner so there's really no point to it.

你真的不需要Java中的模板。当您需要完全不同的类型作为您不知道的参数时,模板主要是有用的。在Java中,所有对象都是虚拟的,并且从一个根对象继承,并且原始类型的定义更加清晰,并且转换以更加敏感的方式工作,因此实际上没有任何意义。

You can get better performance with C++ style templates, but due to the way they generate much more code and the fast CPU speed outstrips data access speed more and more every year this is less and less the case, especially when it comes to objects as opposed to primitives.

使用C ++样式模板可以获得更好的性能,但由于它们生成更多代码的方式和快速的CPU速度每年越来越超过数据访问速度,这种情况越来越少,特别是当涉及到对象时原始人。

#1


45  

Templates as in C++ do not exist in Java. The best approximation is generics.

Java中不存在C ++中的模板。最好的近似是泛型。

One huge difference is that in C++ this is legal:

一个巨大的区别是在C ++中这是合法的:

<typename T> T sum(T a, T b) { return a + b; } 

There is no equivalent construct in Java. The best that you can say is

Java中没有等效的构造。你能说的最好的是

<T extends Something> T Sum(T a, T b) { return a.add(b); }

where Something has a method called add.

Something有一个名为add的方法。

In C++, what happens is that the compiler creates a compiled version of the template for all instances of the template used in code. Thus if we have

在C ++中,会发生的是编译器为代码中使用的模板的所有实例创建模板的编译版本。因此,如果我们有

int intResult = sum(5, 4);
double doubleResult = sum(5.0, 4.0);

then the C++ compiler will compile a version of sum for int and a version of sum for double.

那么C ++编译器将为int编译sum的版本,为double编译sum的版本。

In Java, there is the concept of erasure. What happens is that the compiler removes all references to the generic type parameters. The compiler creates only one compiled version of the code regardless of how many times it is used with different type parameters.

在Java中,存在擦除的概念。会发生什么是编译器删除对泛型类型参数的所有引用。编译器只创建一个编译版本的代码,无论它与不同类型参数一起使用多少次。

Other differences

  • C++ does not allow bounding of type parameters whereas Java does
  • C ++不允许绑定类型参数,而Java则允许

  • C++ allows type parameters to be primitives whereas Java does not
  • C ++允许类型参数为原语,而Java则不允许

  • C++ allows templates type parameters to have defaults where Java does not
  • C ++允许模板类型参数具有Java不具有的默认值

  • C++ allows template specialization whereas Java does not And, as should be expected by this point, C++ style template metaprogramming is impossible with Java generics.
  • C ++允许模板专业化,而Java则不然而且,正如应该预期的那样,Java泛型不可能使用C ++样式模板元编程。

  • Forget about seeing the curiously recurring template pattern in Java
  • 忘记在Java中看到奇怪的重复模板模式

  • Policy-based design is impossible in Java
  • 基于策略的设计在Java中是不可能的

#2


5  

There are no real templates in Java. C++ templates are compile-time entities that are used to generate classes. At runtime, there is no trace of them.

Java中没有真正的模板。 C ++模板是用于生成类的编译时实体。在运行时,没有它们的痕迹。

In Java, there are parameterized types as part of a mechanism called generics. It serves a similar purpose, but is significantly different in how it operates and its implications. It has some representation at runtime, there are specific rules, etc.

在Java中,参数化类型是称为泛型的机制的一部分。它有类似的用途,但在操作方式和含义方面有很大差异。它在运行时有一些表示,有特定的规则等。

Start by reading the Java tutorial, then read Bloch's Effective Java for a detailed description of the caveats if you want to be a "power user".

首先阅读Java教程,然后阅读Bloch的Effective Java,了解如果您想成为“超级用户”的详细说明。

#3


2  

There are no templates in Java. The only thing that is comparable with templates are Java Generics.

Java中没有模板。唯一可与模板相媲美的是Java Generics。

http://java.sun.com/developer/technicalArticles/J2SE/generics/

#4


0  

Java has generics which are similar but not exactly the same as templates. I don't know what the Template interface is but it has nothing to do with C++ templates.

Java具有类似但与模板不完全相同的泛型。我不知道Template接口是什么,但它与C ++模板无关。

#5


0  

There is no build in template mechanism in java. In stead they have generics. Moreover, ides have something called code templates e.g. for eclipse.

java中没有构建模板机制。相反,他们有泛型。此外,ides有一些称为代码模板的东西,例如为了日食。

#6


0  

You don't really need templates in Java. Templates are mainly useful when you need to have completely different types as a parameter that you know nothing about. In Java, all objects are virtual and inherit from one root object, and primitive types are defined much more clearly and casts work in a more sensical manner so there's really no point to it.

你真的不需要Java中的模板。当您需要完全不同的类型作为您不知道的参数时,模板主要是有用的。在Java中,所有对象都是虚拟的,并且从一个根对象继承,并且原始类型的定义更加清晰,并且转换以更加敏感的方式工作,因此实际上没有任何意义。

You can get better performance with C++ style templates, but due to the way they generate much more code and the fast CPU speed outstrips data access speed more and more every year this is less and less the case, especially when it comes to objects as opposed to primitives.

使用C ++样式模板可以获得更好的性能,但由于它们生成更多代码的方式和快速的CPU速度每年越来越超过数据访问速度,这种情况越来越少,特别是当涉及到对象时原始人。