将 boost::Optional 与常量类型一起使用 - C++
我有一个容器类,它使用 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*
工作正常)。
boost::Optional
支持常量类型吗?如果不是,我该如何解决这个问题?- 是否有现成的特征实现可供我使用,而不是定义自己的特征?
任何帮助都会很棒!
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).
- Is
boost::optional
supports constant types? If no, how can I work around this issue? - Is there a ready made traits implementation available which I can use rather than defining my own traits?
Any help would be great!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题不在于
boost::Optional
,而在于您尝试执行的操作的逻辑。首先创建一个 const 容器,然后尝试修改其中包含的内容。如果这有效的话我会感到惊讶。我认为你应该做标准容器(如
vector
)所做的事情并禁止不可复制的模板参数。否则,您将不得不接受这样一个事实:当
T
不可复制时,您的set
方法将无法工作,并提供一个执行初始化的构造函数: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 whenT
is non-copyable, and provide a constructor that performs the initialization:难道
const
特化完全没有意义吗?如果没有专门化,traits
将使用const int
作为value_type
和const int&
作为reference
,这正是您试图通过const
专业化实现的目标,对吧?Isn't the
const
specialization completely pointless? Without the specialization,traits<const int>
will haveconst int
as thevalue_type
andconst int&
as thereference
, which is exactly what you tried to achieve with theconst
specialization, right?