Jon Stewart on 6 Aug 2003 21:39:33 -0000 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
[ALACPP] problem with const |
I have a wrapper class, which contains nothing but a reference as member data. Like all member references, I initialize it upon construction. class Wrapper { public: Wrapper(Foo& f) : m_Foo(f) {} private: Foo& m_Foo; }; (I'm leaving out all the convenience functions which Wrapper provides, as they are irrelevant to the problem.) The problem is, what if I want to construct a Wrapper off of a const Foo reference? It's perfectly acceptable to limit myself to a const Wrapper; nonetheless, this won't compile: const Wrapper w(myConstFooReference); What can you do? Well, since we can't get what we want (const deduction by the compiler), we can either abandon the language entirely or engage in a bit of a work-around: template<class FooType = Foo> class Wrapper { public: Wrapper(FooType& f) : m_Foo(f) {} private: FooType& m_Foo; }; Then, to get the correct const semantics: const Wrapper<const Foo> w(myConstFooReference); Ugly, but it works. Critiques? Jon -- Jon Stewart Advanced Los Angeles C++ stew1@xxxxxxxxxxx http://www.alacpp.org _______________________________________________ alacpp mailing list alacpp@xxxxxxxxxxx http://lists.ellipsis.cx/mailman/listinfo/alacpp