ho la seguente variante del lib spinta:conversione boost :: variant per digitare
typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant;
Ora voglio ottenere un valore da una variabile dichiarata come 'value
' in un struct node
, così ho pensato che potrebbe funzionare in modo generico e chiamare la funzione come tale: find_attribute<long>(attribute);
, tuttavia il compilatore dice che non può eseguire il cast dalla variante al long o qualsiasi altro tipo che gli ho dato. Che cosa sto facendo di sbagliato?
template <typename T>
T find_attribute(const std::string& attribute)
{
std::vector<boost::shared_ptr<node> >::iterator nodes_iter = _request->begin();
for (; nodes_iter != _request->end(); nodes_iter++)
{
std::vector<node::attrib>::iterator att_iter = (*nodes_iter)->attributes.begin();
for (; att_iter != att_iter; (*nodes_iter)->attributes.end())
{
if (att_iter->key.compare(attribute) == 0)
{
return (T)att_iter->value; //even explicit cast doesn't wrok??
//return temp;
}
}
}
}