TITLE: Template arguments and const PROBLEM: stefans@bauv106.bauv.unibw-muenchen.de (Stefan Schwarz) The following peace of code causes trouble when compiling: template class A { private: T _a; public: A(void) {} void set (const T a) { _a = a;} }; main() { A a; const int *x = new int(1); a.set(x); } error: bad argument 1 type for A ::set(): const int * ( const int * expectd) The problem doesn't occur with a class A. PROBLEM: vinoski@apollo.hp.com (Stephen Vinoski), 28 Sep 1992 Hewlett-Packard Parameter "a" to the set member function of the class template is of type "const T". Mentally making the substitution of "int *" for "T", one might come up with: void set(const int *a) {_a = a;} However, one would be wrong. :-) The template instantiation actually results in: void set(int *const a) {_a = a;} Apply const to the type T. In this case, T is "int *", so we end up with "a" being a "const pointer to int". This is completely consistent with and similar to the treatment of typedefs in the language. For example: typedef int *IntPtr; const IntPtr p; declares "p" to be a "const pointer to int", not a "pointer to const int". It might be easier to remind yourself of this behavior if you write the declaration for the set member function like this: void set(T const a) {_a = a;}