Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the code in "The C++ Programming Language Third Edition" on page 854 correct?

Tags:

c++

I try to learn C++. In "The C++ Programming Language Third Edition" book I found code on page 854 (Appendix C.13.1):

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) { /* ... */ }

template<> int X<int>::def_val<int> = 0;
template<> int* X<int>::new_X<int>(int i) { /* ... */ }

I modify it:

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) { return new T(a); }

template<> int X<int>::def_val<int> = 0;
template<> int* X<int>::new_X<int>(int i) { return new int(i); }

But my compiler won't compile it:

1>main.cpp(15): error C2143: syntax error : missing ';' before '<'
1>main.cpp(15): error C2988: unrecognizable template declaration/definition
1>main.cpp(15): error C2059: syntax error : '<'
1>main.cpp(16): error C2143: syntax error : missing ';' before '<'
1>main.cpp(16): error C2470: 'X<T>::new_X' : looks like a function definition, but there is no parameter list; skipping apparent body
1>          with
1>          [
1>              T=int
1>          ]
1>main.cpp(16): error C2988: unrecognizable template declaration/definition
1>main.cpp(16): error C2059: syntax error : '<'
1>main.cpp(19): error C2143: syntax error : missing ';' before '{'
1>main.cpp(19): error C2447: '{' : missing function header (old-style formal list?)
1>
1>Build FAILED.

What wrong code in the book or compiler?

like image 982
ABEgorov Avatar asked Apr 02 '11 08:04

ABEgorov


3 Answers

The code from the book is wrong. The last two lines should read:

template<> int X<int>::def_val = 0;
template<> int* X<int>::new_X(int i) { return new int(i); }
like image 153
Jollymorphic Avatar answered Oct 21 '22 01:10

Jollymorphic


Don't forget to mail Stroustrup. From his homepage:

"As a small token of my gratitude to people who report problems and thereby help me improve the book, I offer a reward to someone who first reports a misspelling, a bug in a code example, or a factual error in the text to me. The bounty for new errors in the text is US$32."

Edit: Oops. You are too late. The bug was fixed in the 19th printing. See here. So no bounty, sorry!

The corrected version looks like this:

template class X {
    // ...
    static T def_val;
    static T* new_X(T a = def_val);
};

template< class T> T X<T>::def_val; // initialize to X<T>()
template< class T> T* X<T>::new_X(T a) { /* ... */ }

template< > int X<int>::def_val = 0;
template< > int* X<int>::new_X(int i) { /* ... */ }
like image 42
Mackie Messer Avatar answered Oct 21 '22 01:10

Mackie Messer


You want

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) { return new T(a); }

template<> int X<int>::def_val = 0;
template<> int* X<int>::new_X(int i) { return new int(i); }
like image 4
ltc Avatar answered Oct 20 '22 23:10

ltc