|
Prev: Does taking address of function template specialization not force instantiation?
Next: Simulating new user-defined operators
From: Bharath on 14 Apr 2008 01:43 Dear all, I'm trying learn templates concept. Here is a line of code from Bjarne Strustrup's C++ programming language 3rd edition: template<class C> struct String<C>::Srep I couldn't understand what the above line of code means. I'm expecting "struct/class <name>" after template<class C>. What exactly String<C>::Srep mean? Does "String" refer to STL's string template? Thanks, Bharath -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Bo Persson on 14 Apr 2008 03:53 Bharath wrote: > Dear all, > I'm trying learn templates concept. > > Here is a line of code from Bjarne Strustrup's C++ programming > language 3rd edition: > > template<class C> struct String<C>::Srep > > I couldn't understand what the above line of code means. I'm > expecting "struct/class <name>" after template<class C>. What > exactly String<C>::Srep mean? Does "String" refer to STL's string > template? No, that would be string or std::string - names are case sensitive. Here String<C>::Srep is the name of a struct, likely declared inside some example String class. It would be found in a defintion outside of the class, like template<class C> struct String<C>::Srep { // contents of Srep goes here }; Bo Persson -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 14 Apr 2008 04:13
On 14 Apr., 18:43, Bharath <tiromarc...(a)gmail.com> wrote: > Dear all, > I'm trying learn templates concept. > > Here is a line of code from Bjarne Strustrup's C++ programming > language 3rd edition: > > template<class C> struct String<C>::Srep > > I couldn't understand what the above line of code means. I'm expecting > "struct/class <name>" after template<class C>. What exactly > String<C>::Srep mean? Does "String" refer to STL's string template? It cannot be the standard library's string class, because this is located in namespace std (and thus would require a leading std:: qualifier) and the full class template name would be std::basic_string. std::basic_string still depends on it's template parameters charT, traits, and allocator and a typedef exists for charT = char and all remaining parameters using some default types as std::string; Without any further knowledge we can say that above declaration specifies a class template String, which depends on some type-parameter C. We can deduce this from the combination of template<class C> with the name String. The struct keyword before String<C>::Srep means that we want to declare an *inner* class struct named Srep inside the class template String. As written this declaration is invalid. We can either declare Srep as part of the class template definition String like this: template<class C> struct String { // Defines outer class (template) String struct Srep; // Declares inner class Srep }; or we can *define* String<C>::Srep out-of-class like this: template<class C> struct String<C>::Srep { //... }; Alternatively, we can also provide the definition of Srep immediately at the point of it's declaration in the hosting class template like this: template<class C> struct String { // Defines outer class (template) String struct Srep { // Defines inner class Srep; //... }; }; Without using templates you can compare your original declaration with something like this: struct StringType { // Defines outer class String struct Srep; // Declares inner class Srep }; // Defines inner class Srep outside of the class StringType: struct StringType::Srep { }; 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! ] |