How generics work in C++/CX
Asked Answered
S

2

8

I couldn't find anything about this on the Internet, so I'm looking for someone with the insights into the way C++/CX is impemented. The Wikipedia article on C++/CX says that it supports runtime-based generics, wich would imply that you don't need a header file with full implementation in order to instantiate a generic type. I undertand how this works for .NET (assemblies contain IL code and JIT can just insert concrete types into it and compile it whenever there's a new instantiation), but in C++/CX (which is natively compiled) there is no JIT to edit the code at runtime (which I asume would be pretty hard for x86 machine code)

So what's the trick here, is it type erasure with boxing or some new contrived scheme?

(I know that the metadata about types is stored in .NET format, I'm after the code in the methods)

Swag answered 17/5, 2012 at 14:20 Comment(3)
Smells like Java's "let's cast everything into Object and then back again" approach to generics.Arrhenius
a runtime based generic would be. C++'s generic type erasure is boost::any, but you shouldnt do runtime generics if you dont have to, and most people never have to. Use templates instead.Decarlo
I think it`s a compile time thing. When an initiation was encountered, like "MyTemplate<RealType> obj;", the compiler will generate a copy of concrete class definition using "MyTemplate" and "RealType", and then uses it to create the object. There is no template thing after compilation.Jovi
S
4

Looking at this article here the reference to generics on the last row specifies that generics are used in C++/CX with interfaces and delegates.

http://msdn.microsoft.com/en-us/library/windows/apps/br212455(v=vs.110).aspx

This makes sense because since it is defined as an interface that allows the C++/CX compiler to compile native code functions for the actual objects, and then use generic interfaces in a fashion similar to C++ templates. The native code for the functions is compiled and the generic interface is used to work with the different types.

For the compiler it seems that this is the difference between C++/CLR and C++/CX. /clr: Generics classes, interfaces & delegates allowed. /ZW: Generic interfaces & delegates only.

If you look here http://msdn.microsoft.com/en-us/library/windows/apps/hh699870(v=vs.110).aspx you will notice that there are no generic rules for the classes.

But if you read this topic http://msdn.microsoft.com/en-us/library/windows/apps/hh755792(v=vs.110).aspx you will notice that the generics are applied as interfaces.

A "generic class" in C++/CX is achieved by using standard C++ templates. The instantiation or the compiler generated specific type of the generic is exported to the metadata, but not the template itself. Thus you can see a MyClass and a MyClass from the metadata but not the original MyClass. This is not true for the generic interface case which is exported to metadata as a generic type.

More information on this can be found here http://en.wikipedia.org/wiki/Windows_Runtime

So to fully answer the question, as of this time, the code in the methods is precompiled native code in the output dll or exe and is attached to actual non generic classes. BUT the code can be used generically by using generic interfaces. So ten different classes can implement IMyInterface and then a variable of type IMyInterface can be used to work with instances of the ten different types for example.

Thus the short answer is there is nothing like full generic classes in C++/CX as there is in C++/CLR. Use templates for the same effect in C++/CX applications. If you must have C++ generics then use a dll made with C++/CLI and work that code from a program compiled as C++/CX.

Note! I gleaned this much from examining various articles and some of them at msdn seem to say they might be subject to change.

Now to use the generic interfaces in C++/CX with templates is probably what they intend. So you make a template called MyClass and then it implements your generic interface called MyInterface, so if you then made a MyClass template instantiation, the new type will automatically implement MyInterface, and this interface can then be used anywhere. So outside of the compiled dll and the header files, other C++/CX modules and files can work with types like MyInterface without needing the header file, because the template instantiation was inside the compiled dll, but a c++ file using the metadata knows how to make the MyInterface type because it has the metadata for MyInterface it, but not the metadata for the MyClass.

In very short there is no generic classes and the generic interface and delegate support in C++/CX is all that actually works like generics in C++/CLI.

Siward answered 17/5, 2012 at 17:15 Comment(1)
Remember that C++/CX is not the same as C++/CLI - the windows runtime is not the same as the CLR and there is functionality in the CLR which is not supported by winrt. You can't assume that CLR functionality will work in a C++/CX winrt applicationDecarbonize
D
3

As far a I know, arbitrary C++/CX Generics are not supported. C++/CX can consume winrt parameterized interfaces, which appear as C++ template specializations, but arbitrary generics cannot be exported.

You can create specializations of parameterized interfaces from the Windows::Foundation namespace, but not original parameterized interfaces (public ref templates).

Decarbonize answered 17/5, 2012 at 18:32 Comment(1)
Yes, the second paragraph is true.Decarbonize

© 2022 - 2024 — McMap. All rights reserved.