How do I do __typeof(__box MyValueType) in the new C++/CLI syntax ?

  • Thread starter Thread starter Bern McCarty
  • Start date Start date
Hi Bern,
How do I do __typeof(__box MyValueType) in the new C++/CLI syntax ?

Since the new C++/CLI doesn't use the __box keyword to do the boxing, I am
afraid there seems not existed an one to one translation, my idea is as the
following:

((MyValueType^)MyValueTypeVar)->GetType()


Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Gary!
Since the new C++/CLI doesn't use the __box keyword to do the boxing, I am
afraid there seems not existed an one to one translation, my idea is as the
following:

((MyValueType^)MyValueTypeVar)->GetType()

The difference between "->GetType()" and "__typeof" is that
"->GetType()" requires an instance of an object while "__typeof" does not.

Therefor the equivalent of "__typeof" in C++/CLI is "::typeid".

See: typeof Goes to T::typeid
http://msdn2.microsoft.com/en-us/library/ms235260(VS.80).aspx

Greetings
Jochen
 
Thanks for the suggestion, Jochen.

So the equivalent expression could be:

MyValueType::typeid


Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
But I don't want the System::Type object that represents the type of MyValueType.
I want the System::Type object that represent the boxed form of MyValueType.
So short of using an instance, how do I get it?

-Bern
 
Bern said:
But I don't want the System::Type object that represents the type of
MyValueType. I want the System::Type object that represent the boxed
form of MyValueType. So short of using an instance, how do I get it?

That's an interesting question. My tip would be this:

tyepdef MyValueType^ MyValueTypeBoxed;
MyValueTypeBoxed::typeid

Example:
typedef System::Int32^ T;
Type^ t = T::typeid;

However, when I do this in VC2003, it prints False:
Console::WriteLine(__typeof(__box int)->IsValueType); // "False"

When I do this in VS2005, it prints True:
int i;
int^ bi = i;
Console::WriteLine(bi->GetType()->IsValueType); // "True"

and this prints True as well:

typedef System::Int32^ T;
Console::WriteLine(T::typeid->IsValueType); // "True"

So it seems to work differently in .NET 2.0. A boxed value type seems to
report the same as the value type itself. This puzzles me...

Tom
 
Hi Bern!
But I don't want the System::Type object that represents the type of
MyValueType. I want the System::Type object that represent the boxed
form of MyValueType. So short of using an instance, how do I get it?

The question is: Why do you need this?

Or what is the background of your question?

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
It is being used it in the overrides of virtual bool Equals(Object __gc*)
in some of our value types:


public: virtual bool Equals(System::Object * obj)
{
if (obj == NULL || __typeof(__box LineStyleID) != obj->GetType())
return false;
....
 
Bern said:
It is being used it in the overrides of virtual bool Equals(Object
__gc*) in some of our value types:


public: virtual bool Equals(System::Object * obj)
{
if (obj == NULL || __typeof(__box LineStyleID) != obj->GetType())
return false;

It looks like it is not possible to get a typeid for a boxed value type
in C++/CLI. I couldn't find a way of doing that, and the draft standard
doesn't specify this either.

But even if you could do that, I don't think your logic is correct.
That's because __box(a_value)->GetType() is not __typeof(__box
ValueType), but __typeof(ValueType) itself, even in VC++ 2003 or with
/clr::OldSyntax:

__value struct MyValueClass
{
int i;
};

int _tmain()
{
MyValueClass v;
__box MyValueClass* boxed_v = __box(v);
Console::WriteLine(boxed_v->GetType()->Equals(
__typeof(MyValueClass)));
// this is True
Console::WriteLine(boxed_v->GetType()->Equals(
__typeof(__box MyValueClass)));
// this is False
}


Tom
 
Hi Bern!
public: virtual bool Equals(System::Object * obj)
{
if (obj == NULL || __typeof(__box LineStyleID) != obj->GetType())
return false;

What's wrong with:

public: virtual bool Equals(System::Object^ obj)
{
if (obj == nullptr || LineStyleID::typeid != obj->GetType())
return false;
return true;
}

???

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Rory said:
They are the same type.

In the old MC++, __typeof(__box MyValueType) != __typeof(MyValueType).
They're two different typeids. I merely noted that the former expression
doesn't seem to exist in C++/CLI.

I agree with Jochen: It's very questionable whether you even need it at
all. If there's a real-world application to such a typeid, then one can
start thinking about how to get that out of C++/CLI. Without a real
need, I wouldn't worry.

Tom
 
Hi Rory!
The form is very correct, but perhaps we'd want to test a bit more before
declaring the two instances of the value type equal... (presuming the '=='
operator isn't overloaded):

if (obj == nullptr || LineStyleID::typeid != obj->GetType())
return false;
return this == static_cast<LineStyleID>(obj);

Note that the cast does an implicit unboxing in C++/CLI - no more __unbox
keyword.

I don't understand your problem...

The following works perfect for me:

value struct Test
{
int a;
int b;
virtual bool Equals(System::Object^ obj) override
{
if (obj == nullptr || Test::typeid != obj->GetType())
return false;
Test t = static_cast<Test>(obj);
if ( (t.a == this->a) && (t.b == this->b) )
return true;
return false;
}
};

int main()
{
Test t1;
Test ^t2 = gcnew Test();

if (t1.Equals(t2))
System::Console::WriteLine("EQ");
}


Or what else do you want?

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Hi Rory!
Sorry - no problem. Note that this is a confirmation of your post's
correctness, since there has been a bit of noise on this thread, I thought
I'd put in the word for this being correct. I just made additional
observations: that you'd want further checking of actual value equality and
not just a "return true" after the type checking and that you don't need any
__unbox operator since the cast does it (and in general you don't need box
casts in C++/CLI).

Upps... sorry...

Greetings
Jochen
 
Back
Top