基于 std::map 和 boost::any: 转换错误的数据存储库
我正在尝试实现一个方便的数据存储库或 我的一个小程序的知识库。 我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
噢,short_n_crisp_exclamatory_word,{!}
我完全忘记了迭代器指向一个
std::pair
!我真丢脸!
抱歉打扰了。
getVal 正确的最后一行是:
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:
Thx, anyway.