From: Marc D. <bdu...@gm...> - 2005-01-20 23:32:17
|
> > template <typename ValueType> > > ValueType convertTo(const ValueType& defVal = ValueType()) const { > > Any::holder<ValueType>* valHolder = 0; > > valHolder = dynamic_cast< Any::holder<ValueType>* >(m_data); > > if(valHolder) { > > return valHolder->value(); > > } > > > > ValueType retVal(defVal); > > std::stringstream ss; > > m_data->output(ss); > > ss >> retVal; > > return retVal; > > This code will only compile if ValueType is i/ostreamable, which is > almost guaranteed to NOT be the case for many uses of Any. We dont neccessarily need the cast in the convertTo template function. Types contained in Any have to be outputable already if you want to << an Any. > > Does anybody see a problem with this? > > Technically i must admit, i don't see a major problem with it. > "Internally", though, i would prefer to keep the distinction between > lexically castable types and "any" type, mainly because of the > guarantees each can/cannot provide. Ok, if everyone hates the idea I would just add a convertTo template function to Any that tries stream based conversion: template <typename ValueType> ValueType convertTo(const ValueType& defVal = ValueType()) const { ValueType retVal(defVal); std::stringstream ss; m_data->output(ss); ss >> retVal; return retVal; } which means that Any can do everything Variant can do except that map<Variant, Variant> is possible while map<Any, Any> is not. Marc |