TITLE: double const in templates (source: comp.std.c++) PROBLEM: dak I've just encountered a problem in one of our template design and wondered what could be done about it. It concerns const reference return value from a templated member function, for example: template class SmartPtr { // ... whatever: public: const T & Dereference (); }; The problem arise when T is instantiated with a Container of constants: SmartPtr a; Here we have a double const which is not supported by the compiler, nor the standard, I believe. Is there a fix or should we abandon all hope of using const returns in templates ? RESPONSE: James Kanze , 26 Mar 96 The draft standard forbids two explicit const's. If the duplicate Const result from typedef's, etc., it's OK. (I don't know if the actual words mention template instantiation types, but if they don't I'm sure that it is an editorial oversight, and not the intent.) RESPONSE: Philippe Verdy <100105.3120@compuserve.com> Declare a temporary template to use types only, but no , then create another template class that will use as the parameter of the first template. To make this class usable for const and non-const references, you can add another template parameter to the first class : one for the case T should be const, one for the other case. Example: template class SmartPtr2 { SmartPtr2(T* p) { mp = const_cast(p) ; } CT & Dereference() { return *mp ; } private : CT *mp ; } ; template class ConstSmartPtr : SmartPtr {} ; template class FreeSmartPtr : SmartPtr {} ; Now you can safely use: FreeSmartPtr and ConstSmartPtr. You cannot use: ConstSmartPtr due to the double const problem; But you can use: FreeSmartPtr no const at all !