The problem I am trying to address arises with making containers such as an std::vector of objects that contain reference and const data members:
You can compose your class of members that take care of those restrictions but are assignable themselves.
#include
template
class readonly_wrapper
{
T value;
public:
explicit readonly_wrapper(const T& t): value(t) {}
const T& get() const { return value; }
operator const T& () const { return value; }
};
struct Foo{};
struct Bar {
Bar (Foo & foo, int num) : foo_reference(foo), number(num) {}
private:
std::reference_wrapper foo_reference; //C++11, Boost has one too
readonly_wrapper number;
// Mutable member data elided
};
#include
int main()
{
std::vector bar_vector;
Foo foo;
bar_vector.push_back(Bar(foo, 10));
};