what does the symbol ^% mean in c++ project [duplicate] - c++-cli

This question already has answers here:
What does the C++/CLI Object^% (caret percent-sign) declaration mean?
(5 answers)
Closed 7 years ago.
i have a c# project AAA with the project type “class library", in another c++ project, it add the AAA.DLL in the reference, in the source code
void CTest:OnCallback(OperationCallbackInfo^% oci)
OperationCallbackInfo is class defined in AAA.dll
my question is: what does the symbol ^ and % mean in the parameter?

It means what you have isn't really C++ at all, but C++/CLI, Microsoft's proprietary version of the language for .NET.
If memory serves, ^% is the syntax for a "tracking reference". It means (at least pretty much) the same as ref does in C#. From a C++ point of view, it's pretty much the same as defining a parameter as a reference to a pointer.

According to this question, it's a "handle", which is a reference (similar to a pointer) in managed C++.

Related

Swift equivalent to ObjC Module Initializers [duplicate]

This question already has answers here:
Is there an alternative to initialize() in macOS now that Swift has deprecated it?
(3 answers)
Swift 3.1 deprecates initialize(). How can I achieve the same thing?
(10 answers)
Closed 4 years ago.
First of all I'm trying to execute code in my app (only from my Swift Framework!) when it is loaded into memory.
Normally I would use this ObjC Method to execute Code when my Framework is loaded into Memory.
Is there something equivalent to this in Swift?
static void __attribute__((constructor)) initialize(void){
NSLog(#"==== Code Injection in Action====");
/*
My Code
*/
}
What I have found:
Apple Developer Page regarding this (but it's also only explained in ObjC)
Other Page about Code Injection in general
Any Ideas?
Currently, Swift does not have this functionality. You can either just define an initialization function for your framework and ask your clients to call it before using any other APIs, or you can just mix in an Objective-C file into the project.

What is the caret (^) for in a generic template in C++? [duplicate]

This question already has answers here:
What does the caret (‘^’) mean in C++/CLI?
(8 answers)
Closed 8 years ago.
I see this code in c++ template examples:
void Main(array<String^>^ args)
but I don't understand the reason for the ^ terminating the String and closing angle bracket.
Is it to allow multiple types or something? If someone could someone straighten me out, I would appreciate it. Thanks!
This code is C++/CLI (C++ .NET). String^ is the equivalent of the String class in C#. It's different from std::string. Furthermore, array is a C++/CLI specific class (it's not a standard STL container).

Is C++/CLI an extension of Standard ISO C++?

Is Microsofts C++/CLI built on top of the C++ Standard (C++98 or C++11) or is it only "similar" and has deviations?
Or, specifically, is every ISO standard conforming C++ program (either C++98 or C++11), also a conforming C++/CLI program?
Note: I interpret the Wikipedia article above only comparing C++/CLI to MC++, not to ISO Standard C++.
Sure, it is an extension to C++03 and can compile any compliant C++03 program that doesn't conflict with the added keywords. The only thing it doesn't support are some of the Microsoft extensions to C++, the kind that are fundamentally incompatible with managed code execution like __fastcall and __try. MC++ was their first attempt at it, kept compatible by prefixing all added keywords with underscores. The syntax was rather forced and not well received by their customers, C++/CLI dropped the practice and has a much more intuitive syntax. Stanley Lippman of C++ Primer fame was heavily involved btw.
The compiler can be switched between managed and native code generation on-the-fly with #pragma managed, the product is a .NET mixed-mode assembly that contains both MSIL and native machine code. The MSIL produced from native C++ source is not exactly equivalent to the kind produced by, say, the C# or VB.NET compilers. It doesn't magically become verifiable and doesn't get the garbage collector love, you can corrupt the heap or blow the stack just as easily. And no optimizer love either, the MSIL gets translated to machine code at runtime and is optimized just like normal managed code with the time restrictions inherent in a jitter. Getting too much native C++ code translated to MSIL is a very common mistake, the compiler hides it too well.
C++/CLI is notable for introducing syntax that got later adopted into C++11. Like nullptr, override, final and enum class. Bit of a problem, actually, it begat __nullptr to be able to distinguish between a managed and a native null pointer. They never found a great solution for enum class, you have to declare it public to get a managed enum type. Some C++11 extensions work, few beyond the ones it already had, auto is fine but no lambda expressions, quite a loss in .NET programming. The language has been frozen since 2005.
The C++/CX language extension is notable as well, one that makes writing C++ code for Store and Phone apps palatable. The syntax resembles C++/CLI a great deal, including the ref class and hats in the syntax. But with objects allocated with ref new instead of gcnew, the latter would have been too misleading. Otherwise very different from C++/CLI at runtime, you get pure native code out of C++/CX. The language extension hides the COM interop code that's underneath, automatically reference-counting objects, translating error codes into exceptions and mapping generics. The resemblance to C++/CLI syntax is no accident, they basically perform the same role. Mapping C++-like syntax to a foreign type system.
CLI is a set of extensions for standard C++. CLI has full support of standard C++ and adds something more. So every C++ program will compile with enabled CLI, except you are using a CLI reserved word and this is the weakness of the extension, because it does not respect the double underscore rule for extensions (such reserved words has to begin with __).
You can deactivate those extensions in the GUI by:
Configuration Properties -> General -> Common Language Runtime Support
Even Bjarne Stroustrup calls CLI an extension:
On the difficult and controversial question of what the CLI binding/extensions to C++ is to be called, I prefer C++/CLI as a shorthand for "The CLI extensions to ISO C++". Keeping C++ as part of the name reminds people what is the base language and will help keep C++ a proper subset of C++ with the C++/CLI extension
Language extensions could always be called deviations from the standard, because it will not compile with a compiler without CLI support (e.g. the ^ pointer).

Is it possible to define a C function at runtime? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Dynamically creating functions in c
Here is an example of what I'd like to do:
void attribute((constructor)) someFunction() {
// Would be nice to define C function "someFunction2" somehow here.
}
I know class_addMethod allows adding C functions to Objective-C classes during runtime.
Is it possible to add C function to C main space?
Please, don't tell me I'm wrong if I'm thinking about this way of doing things - I am interested in it rather for educational purposes.
No. A C function consists of a name and a body. The compiler transforms the body to a binary piece of executable code that will be mapped to some address when a process is created from the executable. The name is used by the static and dynamic linkers as an alias to this address.
At runtime both concepts aren't really of much interest. The executable is loaded and names are resolved, so there's little use in creating them dynamically.
On iOS it would even be impossible to create new function implementations as the kernel disallows to make memory executable.

eval in Objective-C [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there something like Matlab's eval statement in Objective-C 2.0?
Is there an eval function (as in lisp, javascript, python, ruby...) in Objective-C?
What I mean by eval is a function that can take in arbitrary Objective-C code (possibly having class definitions, side effects, instrospection, etc) as a string (not a block or an NSInvocation or IMP or something like that), and evaluate each expression, respecting the current runtime environment state, side effects, class definitions, etc.
If not, is it possible to implement within the confines of the existing runtime?
Neither the language nor Apple's frameworks directly support such a thing. However, one of the goals of LLVM is to be an embeddable compiler suite. I'm pretty sure it can generate executable code right into memory. The hard part would be providing that code with access to the pre-existing environment of the calling code. For example, compiling code which references a local variable or something like that.
(Mind you, this approach is forbidden for the iOS App Store, but it could maybe be workable on Mac OS X.)
Absolutely not. Objective-C is a fully compiled language. Only interpreted languages can do that sort of thing.
No. Code eval is the feature for dynamic language. Although objective-C has dynamic feature , and even Cocoa runtime , it is still considered as a static language (generally).