简体   繁体   English

在CNI / C ++代码中实例化模板类

[英]instantiating a template class in CNI/C++ code

I have been fooling around with CNI in order to interpolate Java and C++ code for the last few hours. 在过去的几个小时中,我一直在鬼混CNI,以便对Java和C ++代码进行插值。

While I was looking the possibility to use the Java collections as a replacement for my old team missing libstd , I tried to create a java.util.Stack object and manipulate it. 当我寻找使用Java集合代替缺少libstd旧团队的libstd ,我尝试创建一个java.util.Stack对象并对其进行操作。

However, the compiler ( gcc and gcj , in that probably the same) seems to play his cheap tricks upon my innocent mind: 但是,编译器( gccgcj ,大概是一样的)似乎在我无辜的头脑中发挥了他的便宜技巧:

# gcc -g -I. test.cc
test.cc: In function ‘int main(int, char**)’:
test.cc:24:3: error: ‘java::util::Stack’ is not a template
   Stack<Person> *stack = new Stack<Person>();
   ^
test.cc:24:30: error: ‘java::util::Stack’ is not a template
   Stack<Person> *stack = new Stack<Person>();

(the Person object is well defined java class) (Person对象是定义良好的java类)

Only when I removed the type argument specification, It let me use the data-structure, as if the type argument was chosen to be java.lang.Object . 仅当我删除了类型参数规范时,它才允许我使用数据结构,就像选择类型参数为java.lang.Object The CNI docs seems to mention nothing about it at all! CNI文档似乎什么也没有提及! no words about templates indeed. 确实没有关于模板的文字。

Does anyone know about using templates in CNI context? 有人知道在CNI上下文中使用模板吗? is it supported? 支持吗? I searched google and stackoverflow for answers, but couldn't come with the slightest clue. 我在googlestackoverflow搜索了答案,但是没有丝毫线索。 Any help will be appreciated. 任何帮助将不胜感激。

In addition, I would like to know if i'm using a gcc 2.95.2 (is there a gcj for that gcc version?) 另外,我想知道我是否正在使用gcc 2.95.2(该gcc版本是否有gcj?)

I am working on Virtual Boxed Ubunto 12 over windows 10 host. 我正在通过windows 10主机开发Virtual Boxed Ubunto 12 More importantly gcj and gcc versions stands on 4.8.4 java --version yields 1.5 and libgcj 4.8.4 更重要的是gcjgcc版本位于4.8.4 java --version产生1.5libgcj 4.8.4

Not a CNI user, but the reason for this one seems to be obvious enough. 不是CNI用户,但这样做的原因似乎很明显。

Generic specification java.util.Stack<T> in Java is only a compile time constraint that checks that you add objects of type compatible with T. Internally, java.util.Stack has only single implementation which accepts all types of Object s (actually, references to all types of objects). Java中的通用规范java.util.Stack<T>只是一个编译时约束,它检查您是否添加了与T兼容的类型的对象。在内部, java.util.Stack仅具有一个接受所有类型Object的实现(实际上是,所有类型的对象) 的引用 You may even trick compiler into addding not an instance of T by using unsafe conversions. 您甚至可以通过使用不安全的转换来欺骗编译器,使其不添加T的实例。 So, java.util.Stack<String> and java.util.Stack<Date> are the same internally. 因此, java.util.Stack<String>java.util.Stack<Date>在内部是相同的。

C++ is different. C ++是不同的。 Instantiation of the same template with different arguments creates different incompatible types. 用不同的参数实例化同一模板会创建不同的不兼容类型。 For example, std::stack<int> , std::stack<int*> and std::stack<char> are totally different implementations and have different code. 例如, std::stack<int>std::stack<int*>std::stack<char>是完全不同的实现,并且具有不同的代码。 C++ template mechanism is a superset of java generics. C ++模板机制是Java泛型的超集。

If, for example, you wanted to get Java functionality in C++, you would write std::stack<void*> even if you wanted to store objects of some particular type T. Type conversion to T has to be done manually. 例如,如果您想获得C ++中的Java功能,则即使您要存储某些特定类型T的对象,也要编写std::stack<void*> 。类型转换为T必须手动完成。

CNI does the same thing. CNI做同样的事情。 It instantiates the only one implementation of java.util.Stack which can accept all types of objects. 它实例化了java.util.Stack的唯一一个实现,该实现可以接受所有类型的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM