boost ::以下代码的任何替代品

时间:2014-01-17 01:21:22

标签: c++ templates boost casting boost-any

我希望摆脱对我的代码的提升依赖性。我有以下结构构造。在代码boost::any_cast中的另一个地方调用和使用此结构时。我知道模板类会这样做,但发现很难编写这个模板。 - C ++ Rookie。

 struct Properties {
 public:
 Properties() {}
 Properties(const std::string &s, const boost::any & p) {
      name = s;
      value = p;
 }

 template <typename T>
 Properties(T n) {
      value = n;
 }
 boost::any value;

 std::string name;
};

2 个答案:

答案 0 :(得分:5)

只是为了好玩,我想我会创建一个简约的任何实现:

//////////////////////////////////////////
// my_any.hpp
#include <memory>
#include <stdexcept>

struct my_any
{
    my_any() = default;
    template <typename T> my_any(T const& v) : _storage(new storage<T>(v)) { }
    my_any(my_any const& other)              : _storage(other._storage? std::move(other._storage->clone()) : nullptr) {}

    void swap(my_any& other)               { _storage.swap(other._storage); }
    friend void swap(my_any& a, my_any& b) { a.swap(b); };
    my_any& operator=(my_any other)        { swap(other); return *this; }

    // todo move semantics
private:
    struct storage_base { 
        virtual std::unique_ptr<storage_base> clone() = 0;
        virtual ~storage_base() = default; 
    };
    template <typename T>
    struct storage : storage_base {
        T value;
        explicit storage(T const& v) : value(v) {}
        std::unique_ptr<storage_base> clone() { return std::unique_ptr<storage_base>(new storage<T>(value)); }
    };
    std::unique_ptr<storage_base> _storage;
    template<typename T> friend T      & any_cast(my_any      &);
    template<typename T> friend T const& any_cast(my_any const&);
};

template <typename T> T& any_cast(my_any& a) { 
    if (auto p = dynamic_cast<my_any::storage<T>*>(a._storage.get()))
        return p->value;
    else
        throw std::bad_cast();
}

template <typename T> T const& any_cast(my_any const& a) { 
    if (auto p = dynamic_cast<my_any::storage<T> const*>(a._storage.get()))
        return p->value;
    else
        throw std::bad_cast();
}

然后,您可以使用它与用例显示的方式完全相同:

struct Properties {
    public:
        Properties(const std::string &s="", const my_any& p={}) 
            : name(s), value(p) {}

        template <typename T> Properties(T n) { value = n; }

        std::string name;
        my_any value;
};

#include <vector>
#include <iostream>

typedef std::vector<Properties> Props;

int main()
{
    Props v;
    v.emplace_back("bye", 42);
    v.emplace_back("vector", v);

    std::cout << "v.size(): "          << v.size()                           << "\n";
    std::cout << "v[0].value: "        << any_cast<int>(v[0].value)          << "\n";
    std::cout << "v[1].value.size(): " << any_cast<Props>(v[1].value).size() << "\n";

    v[0].value = v;

    try {
        std::cout << "v[0].value: " << any_cast<int>(v[0].value) << "\n";
    } catch(std::exception const& e)
    {
        std::cout << e.what() << " exception caught, ok!\n";
    }

    std::cout << "v[0].value.size(): " << any_cast<Props>(v[0].value).size() << "\n";
}

查看输出 Live On Coliru

答案 1 :(得分:1)

boost::any使用类型擦除来存储任何类型的对象,您可以在运行时为其分配不同类型的值。 any_cast用于检索存储在any对象中的正确类型的原始值。例如,您当前的课程允许您这样做

Properties p("int", 42);
std::cout << boost::any_cast<int>(p.value) << '\n';
p = Properties("string", std::string("hello"));
std::cout << boost::any_cast<std::string>(p.value) << '\n';

您不能只将上面的类转换为模板并获得相同的功能。如果这样做,您将只能存储单一类型的值。并且您必须将整个struct更改为模板,而不仅仅是构造函数。

template<typename T>
struct Properties {
 public:
 Properties() {}
 Properties(std::string s, T p)
 : name(std::move(s))   // should use initialization list instead
 , value(std::move(p))  // of assignment within the body
 {}

 Properties(T n)
 : value(std::move(n))
 {}

 std::string name;
 T value;
};

但是,我上面发布的代码现在是非法的。

Properties<int> p("int", 42);
std::cout << p.value << '\n';
// p = Properties<std::string>("string", std::string("hello"));
// will not compile because Properties<int> and Properties<std::string> are
// distinct types 

如果这些限制没有问题,那么修改后的定义应该适合您。