如何从boost中将我自己的类对象插入到ptr_map中.对象是模板化的,所以我不能在地图中使用一些静态类型名称.所以我做了:
ptr_map<string, any> someMap;
Run Code Online (Sandbox Code Playgroud)
我的类继承了boost :: noncopyable.
someMap.insert("Test", new MyClass<SomeTemplate>());
Run Code Online (Sandbox Code Playgroud)
错误是:错误:no matching function for call to ‘boost::ptr_map.
UPD:我更喜欢做一些包装,不要使用boost :: any.所以:
class IWrapper { };
class MyClass : public IWrapper { };
ptr_map<string, IWrapper> someMap;
someMap.insert("Test", new MyClass<SomeTemplate>());
Run Code Online (Sandbox Code Playgroud)
为什么它不起作用(同样的错误)?我可以将继承的类传递给父接口.怎么了?
我使用boost :: any来获得多态类型,我需要能够将一个对象转换为它的基类型.
class A {
public:
int x;
virtual int foo()= 0;
};
class B : public A {
public:
int foo() {
return x + 1;
}
};
int main() {
B* bb = new B();
boost::any any = bb;
bb->x = 44;
A* aa = boost::any_cast<A*>(any);
}
Run Code Online (Sandbox Code Playgroud)
main函数的代码在运行时抛出以下错误:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_any_cast> >'
what(): boost::bad_any_cast: failed conversion using boost::any_cast
Abort trap
Run Code Online (Sandbox Code Playgroud)
如果我在boost :: any_cast代码中为reinterpret_cast更改static_cast,它似乎可行.但是我不确定这会带来什么后果.
你有什么想法?
假设我有一个类Handler,其中包含一些子类,如stringhandler,SomeTypeHandler,AnotherTypeHandler.Handler类将方法"handle"定义为所有子类的公共接口.对于不同的处理程序,"处理"的逻辑完全不同.
所以我需要做的是将任何值传递给handle方法.然后,特定类可以将"任何"转换为他们期望的类型.
基本上我需要的是像java类Object:D
我尝试的第一件事是void*,但显然你做不到B* someB = dynamic_cast<B*>(theVoidPointer),所以那里没有运气.
我的第二个想法是使用boost::any.但是,使用boost :: any的要求是该值必须是copy cunstructable,而我的数据不是这种情况.
有什么想法让这个工作?
谢谢
编辑:请注意,我知道我可以使用一个没有成员的SomeData类,并让我的数据成为其子类,但我正在寻找一种更通用的方法,它不需要我自己创建包装类.
当下面的呼叫失败时,有没有办法知道涉及哪个密钥?
boost::program_options::variables_map vm;
...
int foo_bar = vm["some_key"].as<int>();
Run Code Online (Sandbox Code Playgroud)
如果地图中缺少密钥,或者无法转换为int,我会得到一个相当无法提供信息的bad_any_cast,我无法知道以下任何内容:
我找不到任何解决方案,不涉及修改boost标头或在try..catch块中包装对上面的每个调用.我认为这是一个常见的问题,所以也许其他人知道更好的方法.
我想存储对象中boost::any对象的引用.如何初始化boost :: any对象?我试过了std::ref(),但是boost::any初始化了std::reference_wrapper<>.例如,以下内容
#include <boost/any.hpp>
#include <cxxabi.h>
#include <iostream>
int main(void)
{
int s;
int i = 0;
boost::any x(std::ref(i));
std::cout << abi::__cxa_demangle(x.type().name(), 0, 0, &s) << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
版画
std::reference_wrapper<int>
Run Code Online (Sandbox Code Playgroud)
我想要boost::any包含int&而不是.
我找到了这个https://gist.github.com/2945472,但我需要一个不依赖于c ++ 11的实现.我尝试将它转换为仅使用boost,但我遇到了一些麻烦.
这是我想出的:
#include <boost/any.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/unordered_map.hpp>
struct type_info_hash {
std::size_t operator()(std::type_info const & t) const {
return t.hash_code();
}
};
struct equal_ref {
template <typename T> bool operator()(boost::reference_wrapper<T> a,boost::reference_wrapper<T> b) const {
return a.get() == b.get();
}
};
struct any_visitor {
boost::unordered_map<boost::reference_wrapper<std::type_info const>, boost::function<void(boost::any&)>, type_info_hash, equal_ref> fs;
template <typename T> void insert_visitor(boost::function<void(T)> f) {
try {
fs.insert(std::make_pair(boost::ref(typeid(T)), boost::bind(f, boost::any_cast<T>(boost::lambda::_1))));
} catch (boost::bad_any_cast& e) {
std::cout << e.what() << std::endl;
} …Run Code Online (Sandbox Code Playgroud) 我想知道如果使用boost.any(没有RTTI)类,可以使用任何模板都会降低程序的速度.由于boost any实际上是模板类的包装器,可以说现代编译器优化它会产生相同的效果,对吗?
#include <iostream>
#include <vector>
using namespace std;
template<class T> class tpl
{
T content;
public:
tpl(const T& value) : content(value) {}
operator T() const
{
return content;
}
};
class any
{
public:
any() : content(0) {}
any(const any& other) : content(other.content -> clone()) {}
template<class T> any(const T& value) : content(new holder<T>(value))
{
}
~any()
{
delete content;
}
class placeholder
{
public:
placeholder() {}
virtual placeholder* clone() const = 0;
};
template<class T> class holder …Run Code Online (Sandbox Code Playgroud) 我正在写一个涉及大量模板技巧和boost :: any的库.我遇到了一个我基本上有这个的情况:
boost::any a1, a2, a3, a4;
Run Code Online (Sandbox Code Playgroud)
...我需要调用一个如下所示的函数:
template <typename A1, typename A2, typename A3, typename A4>
void somefunc (A1 a1, A2 a2, A3 a3, A4 a4);
Run Code Online (Sandbox Code Playgroud)
我可以使用一个淫秽嵌套的if语句系列,但假设我正在处理10种不同的类型,那就是10,000 if语句!Boost预处理器可以在这里提供帮助,但这仍然是一个可怕的解决方案.
有没有更好的方法用boost :: any的内容调用模板化函数而不诉诸这种疯狂?据我所知,没有.
我正在尝试存储和操作具有不同参数类型的模板类对象列表; 模板类有两个parametrised方法,一个返回参数类型和一个空隙一个接受它作为输入.
更具体地说,我有一个模板类,定义如下:
template<typename T>
class Test
{
public:
virtual T a() = 0;
virtual void b(T t) = 0;
};
Run Code Online (Sandbox Code Playgroud)
和它的不同规格,如:
class TestInt : public Test<int>
{
public:
int a() {
return 1;
}
void b(int t) {
std::cout << t << std::endl;
}
};
class TestString : public Test<std::string>
{
public:
std::string a() {
return "test";
}
void b(std::string t) {
std::cout << t << std::endl;
}
};
Run Code Online (Sandbox Code Playgroud)
我希望能够在一个列表中存储两者的不同对象TestInt并TestString键入并循环调用一个方法作为另一个方法的输入,如:
for (auto it …Run Code Online (Sandbox Code Playgroud) 我boost::any用来存储指针,并想知道是否有提取多态数据类型的方法.
这是一个理想情况下我想做的简单示例,但目前无效.
struct A {};
struct B : A {};
int main() {
boost::any a;
a = new B();
boost::any_cast< A* >(a);
}
Run Code Online (Sandbox Code Playgroud)
这失败是因为a正在存储B*,而我正在尝试提取A*.有没有办法实现这个目标?
谢谢.