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!