У меня есть std :: map Poco :: Any, который я пытаюсь выполнить итерацию и вывести в поток, но получаю ошибку компилятора. Мой код ниже:
map<string, Poco::Any>::const_iterator it;
map<string, Poco::Any>::const_iterator end = _map.end();
map<string, Poco::Any>::const_iterator begin = _map.begin();
for(it = begin; it != end; ++it) {
const std::type_info &type = it->second.type();
// compile error here:
os << " " << it->first << " : " << Poco::RefAnyCast<type>(it->second) << endl;
}
2 ошибки в этой строке:
'type' cannot appear in a constant-expression
no matching function for call to 'RefAnyCast(Poco::Any&)'
ОБНОВЛЕНИЕ:
Я понимаю, что шаблоны - это время компиляции, тогда как type () - это время выполнения, поэтому работать не будет. Спасибо, что подчеркнули это. Также DynamicAny не будет работать, потому что он принимает только типы, которые имеют реализации DynamicAnyHolder, а не идеально. Единственное правило, которое я хотел бы наложить на типы, - это то, что они << перегружены.
Ниже показано, что я сейчас делаю, в определенной степени работает, но только сбрасывает известные типы, а я не то, что я после.
string toJson() const {
ostringstream os;
os << endl << "{" << endl;
map<string, Poco::Any>::const_iterator end = _map.end();
map<string, Poco::Any>::const_iterator begin = _map.begin();
for(map<string, Poco::Any>::const_iterator it = begin; it != end; ++it) {
const std::type_info &type = it->second.type();
os << " " << it->first << " : ";
// ugly, is there a better way?
if(type == typeid(int)) os << Poco::RefAnyCast<int>(it->second);
else if(type == typeid(float)) os << Poco::RefAnyCast<float>(it->second);
else if(type == typeid(char)) os << Poco::RefAnyCast<char>(it->second);
else if(type == typeid(string)) os << Poco::RefAnyCast<string>(it->second);
else if(type == typeid(ofPoint)) os << Poco::RefAnyCast<ofPoint>(it->second);
else if(type == typeid(ofVec2f)) os << Poco::RefAnyCast<ofVec2f>(it->second);
else if(type == typeid(ofVec3f)) os << Poco::RefAnyCast<ofVec3f>(it->second);
//else if(type == typeid(ofDictionary)) os << Poco::RefAnyCast<ofDictionary>(it->second);
else os << "unknown type";
os << endl;
}
os<< "}" << endl;
return os.str();
}