C++/CLI equivalent to as keyword?

  • Thread starter Thread starter karch
  • Start date Start date
K

karch

How would this C# contruct be represented in C++/CLI? Or is it even
possible?

PolicyLevel level = (enumerator->Current) as PolicyLevel;

Thanks,
Karch
 
karch said:
How would this C# contruct be represented in C++/CLI? Or is it even
possible?

PolicyLevel level = (enumerator->Current) as PolicyLevel;

PolicyLevel* level=dynamic_cast<PolicyLevel*>(enumerator->Current);

The equivalent of a cast in C# is __try_cast.

Arnaud
MVP - VC
 
PolicyLevel* level=dynamic_cast<PolicyLevel*>(enumerator->Current);

The equivalent of a cast in C# is __try_cast.

Arnaud
MVP - VC
is __try_cast c++/CLI or managed c++ ? Or both ?
 
dynamic_cast is the equivalent to the C# "as".

static_cast is the equivalent in 2003 to basic C# casting (__try_cast is
strictly 2003, but is more of a development tool, not a production casting
technique).

safe_cast is the equivalent to basic C# casting in CLI.

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter
 
David said:
dynamic_cast is the equivalent to the C# "as".

static_cast is the equivalent in 2003 to basic C# casting (__try_cast is
strictly 2003, but is more of a development tool, not a production casting
technique).

Why a "developement tool"? It gives you exactly the same behaviour as
C# casting, whereas static_cast result is undefined if the cast is
invalid. static_cast documentation for MC++ states explictely :
"Unchecked pointer casts can break the type safety of __gc pointers,
and cause the garbage collector to fail."

Arnaud
MVP - VC
 
In 'Managed C++ and .NET Development', Stephen Fraser gives a good
explanation for why you should regard __try_cast as a development tool -
basically, __try_cast exceptions will only occur if there are actual mistakes
in your code. But perhaps this is also close to what C# does.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter
 
David said:
In 'Managed C++ and .NET Development', Stephen Fraser gives a good
explanation for why you should regard __try_cast as a development
tool - basically, __try_cast exceptions will only occur if there are
actual mistakes in your code.
We agree; that's the wole point : It's better to have an exception that
unmanaged behaviour and GC failure. Of course, it's still better to have an
error at build time, but you don't always have what you want ;-)

Now, if you are *really* performance worried (as much C++ developpers are,
generally without any measurement to prove that their worries are justified
;-), you can replace __try_cast by static_cast and *pray* not to have an
unnoticed invalid cast that will break hell loose in front of the client.
But perhaps this is also close to what
C# does.
AFAIK, __try_cast and C# casts do strictly the same thing.

Arnaud
MVP - VC
 
Good point Arnaud.
It's apparent that we need to change conversion of the basic C# cast to use
__try_cast for the 2003 target. (It's in today's build).

Our conversion to the CLI target is correct in using safe_cast.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter
 
Arnaud Debaene said:
We agree; that's the wole point : It's better to have an exception that
unmanaged behaviour and GC failure. Of course, it's still better to have
an error at build time, but you don't always have what you want ;-)

Now, if you are *really* performance worried (as much C++ developpers are,
generally without any measurement to prove that their worries are
justified ;-), you can replace __try_cast by static_cast and *pray* not to
have an unnoticed invalid cast that will break hell loose in front of the
client.

AFAIK, __try_cast and C# casts do strictly the same thing.

Arnaud
MVP - VC

__try_cast (MC++) is recast into safe_cast ( C++/CLI) , both generate
exactly the same IL instruction (castclass) just like an ordinary cast in
C#.

Willy.
 
Back
Top