Support for boxed values in value types

In VC++ 2013, you can have a boxed object as a member of a value type. Example code below.

value class Member sealed
    String^ name;
    IBox<int>^ age;

Here’s some code showing how the type can be used.

Member member; = "Nish";
member.age = 80;
int age = member.age->Value;
member.age = nullptr;

Even in normal boxing/unboxing, when you use a safe_cast on a boxed object, eventually what happens is that a safe_cast to IBox<T> is performed and then the Value property accessed. Example, the following two snippets are semantically identical.

int y = 11;

// snippet 1
Object^ obj = y;
int y3 = safe_cast<int>(obj);

// snippet 2
IBox<int>^ boxedY = safe_cast<IBox<int>^>(obj);
int y2 = boxedY->Value;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s