How generics work in C ++ / CX

I couldn't find anything about this on the Internet, so I'm looking for someone who knows how C ++ / CX is impigmented. The C ++ / CX Wikipedia article says that it supports generic generic versions, which means you don't need a header file with a full implementation to create a typical type. I take on how it works for .NET (the assemblies contain IL code, and JIT can just insert specific types into it and compile it whenever there is a new instance), but in C ++ / CX (which is initially compiled ) there is no JIT for editing the code at runtime (which I would rather rather difficult for x86 machine code)

So what's the trick here, is erasing the type with boxing or some kind of new contrived scheme?

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

+6
source share
2 answers

In this article, a link to generics on the last line indicates 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 it is defined as an interface that allows the C ++ / CX compiler to compile native code functions for real objects, and then use common interfaces in a style similar to C ++ templates. Compiled native code for functions and a common interface is used to work with various types.

To the compiler, this seems to be the difference between C ++ / CLR and C ++ / CX. / clr: Classes of classes, interfaces, and delegates are allowed. / ZW: only common interfaces and delegates.

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 general rules for classes.

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

A "common class" in C ++ / CX is achieved using standard C ++ templates. A particular instance or compiler generated by a particular generic type is exported to metadata, but not to the template itself. So you can see MyClass and MyClass from metadata, but not the original MyClass. This does not apply to the case of a generic interface that is exported to metadata as a generic type.

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

So, in order to fully answer the question, from now on, the code in the methods is precompiled native code in the output dll or exe and is tied to actual non-general classes. BUT the code can be used in general terms using common interfaces. Thus, ten different classes can implement IMyInterface, and then a variable of type IMyInterface can be used to work with examples of ten different types, for example.

So the short answer is that in C ++ / CX there is nothing to do with full generic classes, as in C ++ / CLR. Use templates for the same effect in C ++ / CX applications. If you must have C ++ generators, use the dll created using C ++ / CLI and execute this code from a program compiled as C ++ / CX.

Attention! I got it from studying various articles, and some of them in msdn seem to say that they can be changed.

Now using common interfaces in C ++ / CX with templates is probably what they mean. Thus, you create the MyClass template and then implement your common MyInterface interface, so if you create an instance of MyClass, the new type will automatically implement MyInterface, and then this interface can be used anywhere. Therefore, outside the compiled dll and header files, other C ++ / CX modules and files can work with types such as MyInterface without using a header file, because the template instance was inside the compiled dll, but the C ++ file using metadata knows as type MyInterface type as it contains metadata for MyInterface but not metadata for MyClass.

In a very short time there are no common classes, and a common interface and delegate support in C ++ / CX is all that actually works like generics in C ++ / CLI.

+3
source

As far as I know, arbitrary C ++ / CX Generics are not supported. C ++ / CX can consume interfaces with Winrt parameters that appear as specialized C ++ templates, but arbitrary generics cannot be exported.

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

+3
source

Source: https://habr.com/ru/post/915994/


All Articles