Errata: Tips & Tricks to bolster your Managed C++ code in VS.NET

In here I’ll outline the errors and problems I’ve discovered about my MSDN magazine article
(which you can read here).

Unsupported Managed Operators

This is one that Martin Lapierre pointed out to me (Thanks Martin!).

As it turns out, there are three Managed Operators that you can’t overload from your Managed C++ code:

  • op_True (operator true)
  • op_False (operator false)
  • op_LogicalNot (operator!)

Arguably this is a bug, but it is really two kinds of bugs (even if the end result is the same).
The first two operators are not supported at all, while the third one is supported under a different
name.

You would normally define op_True and op_False operators in MC++ like this:


static bool op_True(T * t) { ... }
static bool op_False(T* t) { ... }

The problem is that the compiler won’t recognize these signatures as managed operators, and thus
doesn’t generate the correct metadata alongside them needed for the C# compiler to recognize them.
What’s missing from the generated metadata is the specialname attribute.

The problem with op_LogicalNot, on the other hand, is quite different. The problem is that the
MC++ compiler only recognizes this operator under a name that was used in some of the early .NET betas
and not under the name that the CLR rules dictate. So, if you try declaring it with the name
op_LogicalNot, the MC++ compiler will just treat it as any other static method and generate the normal
metadata associated with it (i.e. not specialname). The end result is the same as for
op_True and op_False.

The name for op_LogicalNot that the MC++ compiler does recognize is op_Negation, and thus,
if you declare the operator with this name, the compiler will generate the correct signature for a
managed operator, but now, it is the C# compiler which doesn’t see the declaration, since it is looking
for a method call op_LogicalNote, which, obviously, doesn’t exist.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>