B
Bern McCarty
In the old MEC++ syntax I can do this:
// compile in VS 2005 shell with cl -clrldsyntax -LD ArrayCopyOldSyntax.cpp
#using <mscorlib.dll>
public __gc class CopyTest
{
private: System::UInt32 m_indeces __gc[];
public: CopyTest(unsigned int indeces __nogc[], unsigned int arrayLength)
{
m_indeces = __gc new System::UInt32[arrayLength];
System::Runtime::InteropServices::Marshal::Copy(System::IntPtr(indeces),
m_indeces, 0, arrayLength);
}
};
And there is a little magic there in that there is no Marshal::Copy overload
that takes an UInt32 __gc[] as an argument for the destination of the copy,
but the compiler happily accepts it and it simply invokes the Copy overload
that takes an Int32 __gc[] as the destination. In other words, it more or
less does an implied reinterpret_cast for you.
But the equivalent class in the new syntax will not compile:
// compile in VS 2005 shell with cl -clr -LD ArrayCopyNewSyntax.cpp
public ref class CopyTest
{
private: array<System::UInt32>^ m_indeces;
public: CopyTest(unsigned int indeces[], unsigned int arrayLength)
{
m_indeces = gcnew array<System::UInt32>(arrayLength);
System::Runtime::InteropServices::Marshal::Copy(System::IntPtr(indeces),
m_indeces, 0, arrayLength);
}
};
ArrayCopyNewSyntax.cpp(11) : error C2665: 'System::Runtime::InteropServices::Marshal::Copy'
: none of the 16 overloads could convert all the argument types
It appears that I can reinterpret_cast m_indeces to array<Int32>^ explicitly
and it will work, and of course I could always write my own loop to do the
copy, but is there a more elegant way to do this in the new syntax? The
Marshal.Copy overloads are very handy for interop code, but getting into
the habit of doing reinterpret casts on object handles doesn't seem like
a good idea.
-Bern McCarty
// compile in VS 2005 shell with cl -clrldsyntax -LD ArrayCopyOldSyntax.cpp
#using <mscorlib.dll>
public __gc class CopyTest
{
private: System::UInt32 m_indeces __gc[];
public: CopyTest(unsigned int indeces __nogc[], unsigned int arrayLength)
{
m_indeces = __gc new System::UInt32[arrayLength];
System::Runtime::InteropServices::Marshal::Copy(System::IntPtr(indeces),
m_indeces, 0, arrayLength);
}
};
And there is a little magic there in that there is no Marshal::Copy overload
that takes an UInt32 __gc[] as an argument for the destination of the copy,
but the compiler happily accepts it and it simply invokes the Copy overload
that takes an Int32 __gc[] as the destination. In other words, it more or
less does an implied reinterpret_cast for you.
But the equivalent class in the new syntax will not compile:
// compile in VS 2005 shell with cl -clr -LD ArrayCopyNewSyntax.cpp
public ref class CopyTest
{
private: array<System::UInt32>^ m_indeces;
public: CopyTest(unsigned int indeces[], unsigned int arrayLength)
{
m_indeces = gcnew array<System::UInt32>(arrayLength);
System::Runtime::InteropServices::Marshal::Copy(System::IntPtr(indeces),
m_indeces, 0, arrayLength);
}
};
ArrayCopyNewSyntax.cpp(11) : error C2665: 'System::Runtime::InteropServices::Marshal::Copy'
: none of the 16 overloads could convert all the argument types
It appears that I can reinterpret_cast m_indeces to array<Int32>^ explicitly
and it will work, and of course I could always write my own loop to do the
copy, but is there a more elegant way to do this in the new syntax? The
Marshal.Copy overloads are very handy for interop code, but getting into
the habit of doing reinterpret casts on object handles doesn't seem like
a good idea.
-Bern McCarty