基于 std::map 和 boost::any: 转换错误的数据存储库

发布于 2024-09-13 16:04:11 字数 1923 浏览 0 评论 0原文

我正在尝试实现一个方便的数据存储库或 我的一个小程序的知识库。 我使用 boost::any 的 std::map 来保存各个部分 信息。为了调试和安全目的,我有 数据的额外安全访问器“getVal()”。

一个片段包含了一千多个单词:

编辑:<<<<旧代码片段被错误的完整再现所取代>>>

#include <map>
#include <boost/any.hpp>
#include <boost/shared_ptr.hpp>
#include <string>
#include <iostream>

typedef std::map<int, boost::any> KnowledgeBase_base;
/**
 *  * KnowledgeBase is simply a storage for information,
 *   * accessible by key.
 *    */
class KnowledgeBase: public KnowledgeBase_base
{
    public:
        template<typename T>
            T getVal(const int idx)
            {
                KnowledgeBase_base::iterator iter = find(idx);
                if(end()==iter)
                {
                    std::cerr << "Knowledgebase: Key " << idx << " not found!";
                    return T();
                }
                return boost::any_cast<T>(*iter);
            }

        bool isAvailable(int idx)
        {
            return !(end()==find(idx));
        }

    private:
};

int main(int argc, char** argv)
{
    KnowledgeBase kb;
    int i = 100;
    kb[0] = i;
    kb[1] = &i;

    std::cout << boost::any_cast<int>(kb[0]) << std::endl; // works
    std::cout << *boost::any_cast<int*>(kb[1]) << std::endl; // works
    std::cout << kb.getVal<int>(0) << std::endl; // error
    std::cout << kb.getVal<int*>(1) << std::endl; // error
    std::cout << "done!" << std::endl;
        return 0;
}

当我在其中存储Something*,并尝试
编辑:如上面更新的示例代码所示,它不需要是指针! Something* = kb->getVal 它抱怨: boost::exception_detail::clone_impl >' What(): boost::bad_any_cast: 使用 boost::any_cast 转换失败

如果我使用 KnowledgeBase::operator[] 它可以工作。 你能告诉我出了什么问题吗?

I am trying to implement a handy data repository or
knowledge base for a little program of mine.
I use a std::map of boost::any's to hold various pieces of
information. For debugging and safety purposes, I have
an extra safe accessor ''getVal()'' for the data.

A snippet says more than a thousand words:

EDIT: <<< Old snippet replaced by complete reproduction of error >>>

#include <map>
#include <boost/any.hpp>
#include <boost/shared_ptr.hpp>
#include <string>
#include <iostream>

typedef std::map<int, boost::any> KnowledgeBase_base;
/**
 *  * KnowledgeBase is simply a storage for information,
 *   * accessible by key.
 *    */
class KnowledgeBase: public KnowledgeBase_base
{
    public:
        template<typename T>
            T getVal(const int idx)
            {
                KnowledgeBase_base::iterator iter = find(idx);
                if(end()==iter)
                {
                    std::cerr << "Knowledgebase: Key " << idx << " not found!";
                    return T();
                }
                return boost::any_cast<T>(*iter);
            }

        bool isAvailable(int idx)
        {
            return !(end()==find(idx));
        }

    private:
};

int main(int argc, char** argv)
{
    KnowledgeBase kb;
    int i = 100;
    kb[0] = i;
    kb[1] = &i;

    std::cout << boost::any_cast<int>(kb[0]) << std::endl; // works
    std::cout << *boost::any_cast<int*>(kb[1]) << std::endl; // works
    std::cout << kb.getVal<int>(0) << std::endl; // error
    std::cout << kb.getVal<int*>(1) << std::endl; // error
    std::cout << "done!" << std::endl;
        return 0;
}

When I store a Something* in it, and try a
EDIT: as visible in the updated example code above, it doesn't need to be a pointer!
Something* = kb->getVal it complains with:
boost::exception_detail::clone_impl >'
what(): boost::bad_any_cast: failed conversion using boost::any_cast

If I use KnowledgeBase::operator[] it works.
Can you tell me what is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如狂蝶 2024-09-20 16:04:11

噢,short_n_crisp_exclamatory_word,{!}

我完全忘记了迭代器指向一个 std::pair
我真丢脸!

抱歉打扰了。

getVal 正确的最后一行是:

        return boost::any_cast<T>(iter->second);
    }

Thx,无论如何。

Oh short_n_crisp_exclamatory_word,{!}

I completely forgot that the iterator points to a std::pair !
Shame on me!

Sorry for bothering.

The correct last line of getVal is:

        return boost::any_cast<T>(iter->second);
    }

Thx, anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文