Occasionally, I write some managed C++ code as a glue-layer between C++ and F#. Today was such an occasion, and I found myself writing the same piece of code in several places with different types. Obviously, my reaction was to refactor to share the common code – given that the behaviour was common and independent of the underlying type, a template class seemed appropriate – but can you use templates with managed C++?
It turns out that you can – here’s the code, the aim of which was to take some COM object from native code and wrap it as an option of some expected strong type. If you haven’t seen managed C++ before, the syntax looks pretty ghastly – it may help to mentally substitute & for ^. If you aren’t familiar with F# option, it’s like boost::optional;
.
template<typename T> FSharpOption<T^>^ getOptional( IUnknown* raw ) { FSharpOption<T^>^ optionalValue = optionalValue = FSharpOption<T^>::None if ( raw != nullptr ) { T^ cooked = (T^)Marshal::GetObjectForIUnknown(IntPtr( raw )); if (cooked != nullptr) optionalValue = FSharpOption<T^>::Some(cooked); } return optionalValue; }
Here’s how you would call the template function to get back the managed C++ equivalent of the F# type MyType option
:
FSharpOption<MyType^>^ myValue = getOptional<MyType>( _rawValue );