-1
比方说,我有这包括:有没有办法让这个更短更清晰?
#include <map>
#include <boost/any.hpp>
#include <string>
,代码:
int main()
{
typedef std::map< std::string, boost::any > table;
table foobar;
foobar["foo"] = table();
table* foobarfoo = boost::any_cast< table* >(foobar["foo"]);
(*foobarfoo)["bar"] = int(5);
int* test = boost::any_cast< int* >((*foobarfoo)["bar"]);
}
这工作得很好,虽然它并没有真正好看,尤其是当我需要从map(int* test here)
带指针在一行中。 我真的很想在这里看到的是这样的事情:
int main()
{
typedef std::map< std::string, boost::any > table;
table foobar;
foobar["foo"] = table();
foobar["foo"]["bar"] = int(5);
int* test = boost::any_cast< int* >(foobar["foo"]["bar"]);
}
这看起来很多,更清晰,但它不会这样的。我的问题是,是否有办法让第二个代码有点小修改,或者有一种方法看起来和第二个例子一样好,但仍然有效?
“更短”和“更清晰”是互斥的。 :P – Casey