将 boost::Optional 与常量类型一起使用 - C++

发布于 2024-08-22 12:46:24 字数 1263 浏览 15 评论 0原文

我有一个容器类,它使用 boost::Optional 来保存该值。代码如下所示,

template<typename T>
struct traits
{
    typedef T  value_type;
    typedef T& reference;
};

template<typename T>
struct traits<const T>
{
    typedef const T  value_type;
    typedef const T& reference;
};

template<typename T>
struct traits<T*>
{
    typedef T* value_type;
    typedef T* reference;
};

template<typename T>
struct traits<const T*>
{
    typedef const T* value_type;
    typedef const T* reference;
};

template<typename T>
class container
{
public:

    typedef typename traits<T>::reference reference;
    typedef typename traits<T>::value_type value_type;

    container() {}

    void set(reference value) {
        op.reset(value);
    }

    reference get() const {
        return boost::get(op);
    }

private:
    boost::optional<value_type> op;
};

int main()
{
    foo f;
    container<const foo> c;
    c.set(f);
    return 0;
}

它适用于除 const 之外的其他类型。当我使用 const 类型时出现错误(const foo* 工作正常)。

  1. boost::Optional 支持常量类型吗?如果不是,我该如何解决这个问题?
  2. 是否有现成的特征实现可供我使用,而不是定义自己的特征?

任何帮助都会很棒!

I have a container class which uses boost::optional to hold the value. Here is the code looks like,

template<typename T>
struct traits
{
    typedef T  value_type;
    typedef T& reference;
};

template<typename T>
struct traits<const T>
{
    typedef const T  value_type;
    typedef const T& reference;
};

template<typename T>
struct traits<T*>
{
    typedef T* value_type;
    typedef T* reference;
};

template<typename T>
struct traits<const T*>
{
    typedef const T* value_type;
    typedef const T* reference;
};

template<typename T>
class container
{
public:

    typedef typename traits<T>::reference reference;
    typedef typename traits<T>::value_type value_type;

    container() {}

    void set(reference value) {
        op.reset(value);
    }

    reference get() const {
        return boost::get(op);
    }

private:
    boost::optional<value_type> op;
};

int main()
{
    foo f;
    container<const foo> c;
    c.set(f);
    return 0;
}

It works well for other types except const. I am getting error when I use const types (const foo* works fine).

  1. Is boost::optional supports constant types? If no, how can I work around this issue?
  2. Is there a ready made traits implementation available which I can use rather than defining my own traits?

Any help would be great!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒冷纷飞旳雪 2024-08-29 12:46:25

问题不在于 boost::Optional,而在于您尝试执行的操作的逻辑。首先创建一个 const 容器,然后尝试修改其中包含的内容。如果这有效的话我会感到惊讶。

我认为你应该做标准容器(如vector)所做的事情并禁止不可复制的模板参数。

否则,您将不得不接受这样一个事实:当 T 不可复制时,您的 set 方法将无法工作,并提供一个执行初始化的构造函数:

class container
{
public:

    container(reference init_value) : op(init_value) {}

};

int main()
{
    foo f;
    container<const foo> c(f);  // OK
    //   c.set(f);  NO
    return 0;
}

The problem is not with boost::optional, but with the logic of what you're trying to do. First you create a container of const, and then you try to modify what's contained. I would be surprised if that worked.

I think you should probably do what standard containers (like vector) do and forbid non-copyable template arguments.

Otherwise you'll have to live with the fact that your set method won't work when T is non-copyable, and provide a constructor that performs the initialization:

class container
{
public:

    container(reference init_value) : op(init_value) {}

};

int main()
{
    foo f;
    container<const foo> c(f);  // OK
    //   c.set(f);  NO
    return 0;
}
皓月长歌 2024-08-29 12:46:25
template<typename T>
struct traits
{
    typedef T  value_type;
    typedef T& reference;
};

template<typename T>
struct traits<const T>
{
    typedef const T  value_type;
    typedef const T& reference;
};

难道 const 特化完全没有意义吗?如果没有专门化,traits 将使用 const int 作为 value_typeconst int& 作为reference,这正是您试图通过 const 专业化实现的目标,对吧?

template<typename T>
struct traits
{
    typedef T  value_type;
    typedef T& reference;
};

template<typename T>
struct traits<const T>
{
    typedef const T  value_type;
    typedef const T& reference;
};

Isn't the const specialization completely pointless? Without the specialization, traits<const int> will have const int as the value_type and const int& as the reference, which is exactly what you tried to achieve with the const specialization, right?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文