【问题标题】:Unable to iterate std::map of Poco::Any无法迭代 Poco::Any 的 std::map
【发布时间】:2011-06-22 05:18:42
【问题描述】:

我有一个 Poco::Any 的 std::map,我正在尝试迭代并输出到流中,但出现编译器错误。我的代码如下:

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();
}

【问题讨论】:

    标签: c++ dictionary stl poco-libraries any


    【解决方案1】:

    运行时类型信息不能用于实例化模板。 type_info 的实例只有在您运行程序时才能知道其值,当编译器编译此代码时,它不会神奇地变成像 intstd::stringstruct FooBar 这样的类型。

    我不知道 Poco 库,但也许您可以使用他们的其他 Any 类型,DynamicAny(请参阅documentation),它有望让您将存储的值转换为 std::string 以进行输出:

    os << "  " << it->first << " : " << it->second.convert<std::string>() << endl;
    

    【讨论】:

    • +1。也许应该强调的是,C++ 模板是编译时的东西,模板参数必须在编译时知道。看到 OP 无法找出问题所在,这对 OP 来说可能是个新闻。
    • “也许应该强调 C++ 模板是编译时的东西,模板参数必须在编译时知道”。是的,这完全清楚了,谢谢。不幸的是,DynamicAny 并不真正支持 Any 类型,只支持那些有 DynamicAnyHolder 实现的类型,因此不支持确实有
    • @Memo:不幸的是,只有底层的具体值持有类(在 boost holder&lt;T&gt; 中)知道如何输出持有的值。似乎没有办法让那个人接受访问者,所以唯一的选择可能是添加一个输出值的方法,或者至少是一个to_string 方法。 codepad.org/NICm5r2r 只是将boost::any 修改为具有to_string 方法(stringifier&lt;T&gt; 是为了增加一种服装化的可能性,但boost::lexical_cast 的默认实现应该适用于除@987654335 之外的所有“cout-able”对象@.
    猜你喜欢
    • 2012-03-04
    • 2012-01-06
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多