0

Stan Lippman et. al (in "C++ Primer ", 5/e) , say :

  1 // forward declarations needed for friend declarations in Blob
  2 template <typename> class BlobPtr;
  3 template <typename> class Blob; // needed for parameters in operator==
  4 template <typename T>
  5     bool operator==(const Blob<T>&, const Blob<T>&);
  6 template <typename T> class Blob {
  7     // each instantiation of Blob grants access to the version of
  8     // BlobPtr and the equality operator instantiated with the same type
  9     friend class BlobPtr<T>;
 10     friend bool operator==<T>
 11            (const Blob<T>&, const Blob<T>&);
 12     // other members as in § 12.1.1 (p. 456)
 13 };

I didn't really understand template <typename> part in line 2 & 3. I tried to search, but couldn't find anything clear and substantial to read more about about template <typename>.

Please help me with some pointers for reading more about declarations such as

template <typename> class BlobPtr
Onkar N Mahajan
  • 410
  • 3
  • 13
  • 3
    It's a forward class direction, albeit a template one. Just like forward declarations of regular classes or functions. – kabanus Nov 24 '20 at 12:43
  • 2
    name is omitted, it might be `template class BlobPtr;`, as for regular function `void foo(int, char)`. – Jarod42 Nov 24 '20 at 12:45
  • Actually that's what I also guessed, but even in that case, I didn't get how just typename is going to be helpful here. – Onkar N Mahajan Nov 24 '20 at 12:48
  • 3
    "I tried to search" So, when you put `template c++` into a search engine, what results did you get? What did you not understand about what you read there? For me, the first result was [this](https://www.cplusplus.com/doc/oldtutorial/templates/). Do you find this unclear or unsubstantial (make sure you scroll down to where it says "class templates")? Could you ask a more specfic question? – Karl Knechtel Nov 24 '20 at 12:48
  • 2
    `typename` is "helpful" i.e. needed because you are telling the compiler that the template will have a type argument in that position... Otherwise, it won't know how many template arguments there are, nor whether they are types or values. The name can be omitted because it's only a forward-declaration and hence there's no use for a name. I too think this should be covered by most fairly introductory reading on templates. – underscore_d Nov 24 '20 at 12:50
  • 3
    If you are trying to understand C++, "trying to search" will not accomplish anything. C++ is the most complicated general purpose programming language in use today, and not much can be learned from Google searches, Youtube videos, or web sites with silly programming puzzles and contests on them. The only way you will learn what all of the above means, will be [in a C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Nov 24 '20 at 12:57

1 Answers1

3
template <typename> class BlobPtr;

This is declaration of a class template. The name of the template is BlobPtr and it has one template type parameter.

eerorika
  • 232,697
  • 12
  • 197
  • 326