Note: I\'m aware of boost::variant
, but I am curious about the design principles. This question mostly for self-education.
This implementation is close to correct, but it looks like it has a few bugs. For example, this code:
if (typeid(T).name() != mClassName)
is not guaranteed to work correctly because the .name()
function in type_info
is not guaranteed to return a unique value for each type. If you want to check if the types match, you should probably use something like this:
if (typeid(*mImpl) == typeid(VariantImpl<T>))
Which more accurately checks if the type matches. Of course, you need to watch out for const
issues, since storing a const T
and storing a T
will yield different types.
As for your question about dynamic_cast
, in the case you've described you don't need to use the dynamic_cast
because you already have a check to confirm that the type will match. Instead, you can just use a static_cast
, since you've already caught the case where you have the wrong type.
More importantly, though, what you've defined here is an "unrestricted variant" that can hold absolutely anything, not just a small set of restricted types (which is what you'd normally find in a variant). While I really like this code, I'd suggest instead using something like Boost.Any or Boost.Variant, which has been extensively debugged and tested. That said, congrats on figuring out the key trick that makes this work!
At the risk of providing a non-answer, since you are already using Boost, I recommend you try Boost.Variant or Boost.Any instead of rolling your own implementation.
Better to use std::auto_ptr
, as there's no reference counting semantics required. I would normally return by reference as it's perfectly legal to change the value within, or by pointer to allow NULL.
You should use dynamic_cast
to match types, not typeid()
, and you could just use Boost. typeid()
seems like it should provide this but in reality it doesn't because of the open-endedness of it's specification, whereas dynamic_cast
is always exactly and unambiguously correct.