标签: boost-any

提升任何用途

如何从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)

为什么它不起作用(同样的错误)?我可以将继承的类传递给父接口.怎么了?

c++ boost insert map boost-any

7
推荐指数
1
解决办法
3440
查看次数

如何使用boost :: any_cast(c ++库)转换为基类型?

我使用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,它似乎可行.但是我不确定这会带来什么后果.

你有什么想法?

c++ casting boost-any

6
推荐指数
1
解决办法
1万
查看次数

如何将"任何类型的数据"传递给C++中的函数

假设我有一个类Handler,其中包含一些子类,如stringhandler,SomeTypeHandler,AnotherTypeHandler.Handler类将方法"handle"定义为所有子类的公共接口.对于不同的处理程序,"处理"的逻辑完全不同.

所以我需要做的是将任何值传递给handle方法.然后,特定类可以将"任何"转换为他们期望的类型.

基本上我需要的是像java类Object:D

我尝试的第一件事是void*,但显然你做不到B* someB = dynamic_cast<B*>(theVoidPointer),所以那里没有运气.

我的第二个想法是使用boost::any.但是,使用boost :: any的要求是该值必须是copy cunstructable,而我的数据不是这种情况.

有什么想法让这个工作?

谢谢

编辑:请注意,我知道我可以使用一个没有成员的SomeData类,并让我的数据成为其子类,但我正在寻找一种更通用的方法,它不需要我自己创建包装类.

c++ pointers boost-any

6
推荐指数
1
解决办法
369
查看次数

更好地处理boost :: program_options中的丢失/错误键

当下面的呼叫失败时,有没有办法知道涉及哪个密钥?

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块中包装对上面的每个调用.我认为这是一个常见的问题,所以也许其他人知道更好的方法.

c++ boost boost-program-options boost-any

5
推荐指数
1
解决办法
726
查看次数

如何通过引用对象来初始化boost :: any?

我想存储对象中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&而不是.

c++ boost-any c++11

5
推荐指数
1
解决办法
1806
查看次数

boost :: any的访客模式

我找到了这个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)

c++ boost visitor-pattern boost-any

5
推荐指数
1
解决办法
1998
查看次数

C++性能:模板vs boost.any

我想知道如果使用boost.any(没有RTTI)类,可以使用任何模板都会降低程序的速度.由于boost any实际上是模板类的包装器,可以说现代编译器优化它会产生相同的效果,对吗?

tpl_vs_any.hpp

#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)

c++ templates boost boost-any

5
推荐指数
1
解决办法
4995
查看次数

boost :: any和模板

我正在写一个涉及大量模板技巧和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的内容调用模板化函数而不诉诸这种疯狂?据我所知,没有.

c++ templates boost-any

5
推荐指数
1
解决办法
1347
查看次数

使用不同的模板参数在列表项上调用参数化方法

我正在尝试存储和操作具有不同参数类型的模板类对象列表; 模板类有两个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)

我希望能够在一个列表中存储两者的不同对象TestIntTestString键入并循环调用一个方法作为另一个方法的输入,如:

for (auto it …
Run Code Online (Sandbox Code Playgroud)

c++ templates boost boost-any

5
推荐指数
1
解决办法
137
查看次数

Boost :: any和polymorphism

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*.有没有办法实现这个目标?

谢谢.

c++ polymorphism boost-any

4
推荐指数
1
解决办法
2221
查看次数