When you create an instance of a reference type using stack semantics, the compiler does internally create the instance on the garbage collected heap (using gcnew).
When the signature or return type of a function includes an instance of a by-value reference type, the function will be marked in the metadata as requiring special handling (with modreq). This special handling is currently only provided by Visual C++ clients; other languages do not currently support consuming functions or data that use reference types created with stack semantics.
One reason to use gcnew (dynamic allocation) instead of stack semantics would be if the type has no destructor. Also, using reference types created with stack semantics in function signatures would not be possible if you want your functions to be consumed by languages other than Visual C++.
The compiler will not generate a copy constructor for a reference type. Therefore, if you define a function that uses a by-value reference type in the signature, you must define a copy constructor for the reference type. A copy constructor in a reference type can have the following signature: R(R%){}.
The compiler will not generate a default assignment operator for a reference type. An assignment operator allows you to create an object using stack semantics and initialize it with an existing object created using stack semantics. An assignment operator in a reference types can have the following signature: void operator=( R% ){}.
If your type's destructor releases critical resources and you use stack semantics for reference types, you do not need to explicitly call the destructor (or call delete). For more information on destructors in reference types, see Destructors and Finalizers in Visual C++.
A compiler-generated assignment operator will follow the usual standard C++ rules with the following additions:
Any non-static data members whose type is a handle to a reference type will be shallow copied (treated like a non-static data member whose type is a pointer).
Any non-static data member whose type is a value type will be shallow copied.
Any non-static data member whose type is an instance of a reference type will invoke a call to the reference type’s copy-constructor.
The compiler also provides a % unary operator to convert an instance of a reference type created using stack semantics to its underlying handle type.
The following reference types are not available for use with stack semantics: