Unofficial Managed C++ FAQ

List of entries

  1. Compiler switches incompatible with /clr

  2. Why can’t I use the overloaded operators on type ‘X’
  3. Can I overload managed operators on my MC++ classes?
  4. How can I define a method that takes an ‘ref’ argument?
  5. How can I define a method that takes an array of ints by
    reference?
  6. Why do I always get an error when trying to add an
    attribute to an enum value?

Last Modified: June 1, 2002


Compiler switches incompatible with /clr

The following options are incompatible with the /clr compiler
switch:

  • /GL
    – Link Time Code Generation.

  • /Zd
    – Generate COFF debug information.

  • /ZI
    – Support "Edit and Continue&1uot; in debug information.

  • /ML (/MLd)
    – Link statically to the CRT (debug).

  • /Gm
    – Enable Minimal rebuild.

  • /YX
    – Automatic use of precompiled headers.

  • /RTCc
    – Runtime error check for possible data loss in conversion.

  • /RTCs
    – Stack Frame runtime error checking.

  • /RTCu
    – Runtime error check for use of unintialized variable.

  • /RTC1
    – Equivalent to /RTCsu.

Why can’t I use the overloaded operators on type ‘X’

If type ‘X’ is a reference object, you won’t be able to consume any of the
overloaded managed operators it might have defined. This is because there’s a
real impedance mismatch between operator overloading and the fact that in MC++
reference objects have to be accessed through pointers. Since standard C++,
from which MC++ sort of came out, explicitly defines semantics for standard
operators on pointers, such as the meaning of order relations (<, >, =, etc.),
the use of actual operators would conflict with them.

Note that you should be able to use some overloaded operators on value types,
though.


Can I overload managed operators on my MC++ classes?

Yes, using the appropriate operator name (such as op_Addition.
However, not all managed operators defined by the CLR can be overloaded on the
first release of MC++, because of compiler bugs. Look
here
for detailed information on those.


How can I define a method that takes an ‘ref’ argument?

If you want to define a method that takes a reference argument (that is, and
in/out argument) that C# programs can see defined as “ref”, you can use the C++
reference (&) syntax.

However, if the type of the argument is one of the basic types, and you try to
use the C++ name for the type, this won’t compile at first. For example, this
won’t work:


      void MethodWithRef(int& v);
      

The simplest workaround is to use the CLR’s typename, instead:


      void MethodWithRef(System::Int32& v);
      

How can I define a method that takes an array of ints by reference?

Try something like this:


      void MethodWithRef(System::Int32 (&intArray) __gc[]) {
         const size_t arraySize = 2;
         intArray = new int __gc[arraySize];
         // fill array
      }
      

Why do I always get an error when trying to add an attribute to an enum value?

You’re probably doing something like this:


      public __gc class MyCustomAttribute : public Attribute
      {
         public:
            MyCustomAttribute(String* desc) {
            }
         // rest of implementation
      };

      __value enum MyEnum {
         [ MyCustomAttribute(S"Value 1") ] 
         value_1 = 1,
         [ MyCustomAttribute(S"Value 2") ] 
         value_2 = 2,
      };
      

And you’re getting a C2143 and a few other errors, right? The cause is a bug in
the current MC++ compiler that doesn’t allow it to parse attributes in an enum
declaration, so there’s no workaround for it.