Re: template<> compile error
On 17 Dez., 22:36, Coltrane <tendenga...@yahoo.com> wrote:
I am trying to compile some code from the "C++ Programming Language
Special Edition" book and I am getting compiler errors with Visual C++
and g++. The sample code is in appendix C, section C.13.
The code is as follows:
template<class T>
class X{
static T def_val;
static T* new_X(T a = def_val);
};
template<class T> T X<T>::def_val(0,0);
template<class T> T* X<T>::new_X(T a) {}
A function with non-void result type is
required to have a return clause. I assume
you wrote
template<class T> T* X<T>::new_X(T a) {return new T(a);}
instead (The book uses the abbreviated notion of
/* ... */ to specify that).
template<> int X<int>::def_val<int> = 0; /*line 10 */
template<> int* X<int>::new_X<int>(int i){} /* line 11 */
I get the following errors with Visual C++
[..]
Indeed these member definitions are invalid and
both compilers are correct to reject the code
(again: I assume a missing return in new_X). The
proper definitions of these so-called explicit
member specializations are:
template<> int X<int>::def_val = 0;
template<> int* X<int>::new_X(int i){/*...*/}
[Note the missing trailing <..>]
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]