ElementTypeT& UnknownElement::ConvertTo() { CastVisitor_T<ElementTypeT> castVisitor; m_pImp->Accept(castVisitor); if (castVisitor.m_pElement == 0) { // we're not the right type. fix it & try again *this = ElementTypeT(); m_pImp->Accept(castVisitor); } return *castVisitor.m_pElement; }
ElementTypeT& UnknownElement::CastTo() { CastVisitor<ElementTypeT> castVisitor; Accept(castVisitor); // If this element is uninitialized, implicitly convert it to the desired type if (castVisitor.is_null) { *this = ElementTypeT(); return *this; } // Otherwise throw an exception if (!castVisitor.element) throw Exception("Bad cast"); return *castVisitor.element; }