Why do members of a template class need to be parameterized by the parameters of their template class

The name of the picture


Why do members of a template class need to be parameterized by the parameters of their template class



In page 668 of Stroustrup's book (4th edition - first printing) you`ll find the following example of a template class?


template<typename C>
class String{
public:
String();
...
private:
int sz;
C* ptr;
};



In page 679 the author writes:



Members of a template class are themselves templates parameterized by
the parameters of their template class. When such a member is defined
outside its class, it must explicitly be declared as template. For
example:


template<typename C>
String<C>::String()
:sz(0), ptr(ch)
{
ch[0] = {};
}



There is an obvious error in this example. The variable ch doesn't make any sense above. But that has nothing to do with my question. What I'd like to know is why the constructor above can't be defined without the parameter C, as shown below?


ch


C


template<typename C>
String::String()
: sz(0), ptr(nullptr)
{
}









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

Future solutions