fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //прототипы шаблонов
  5. template <typename T> void counts();
  6. template <typename T> void report(T &);
  7.  
  8. //шаблонный класс
  9. template <typename TT>
  10. class HasFriendT
  11. {
  12. private:
  13. TT item;
  14. static int ct;
  15. public:
  16. HasFriendT(const TT & i) : item(i) {ct++;}
  17. ~HasFriendT( ) {ct--;}
  18. friend void counts<TT>();
  19. friend void report<>(HasFriendT<TT> &);
  20. };
  21. template <typename T>
  22. int HasFriendT<T>::ct = 0;
  23.  
  24. //определение дружественных функции для шаблона
  25. template <typename T>
  26. void counts()
  27. {
  28. cout << "template size: " << sizeof(HasFriendT<T>) << "; "; //размер шаблона
  29. cout << "template counts(): " << HasFriendT<T>::ct << endl;
  30. }
  31. template <typename T>
  32. void report(T & hf)
  33. {
  34. cout << hf.item << endl;
  35. }
  36.  
  37. int main()
  38. {
  39. counts<int>();
  40. HasFriendT<int> hfi1(10);
  41. HasFriendT<int> hfi2(20);
  42. HasFriendT<double> hfdb(10.5);
  43. report(hfi1);
  44. report(hfi2);
  45. report(hfdb);
  46. cout << "counts<int>() output:\n";
  47. counts<int>();
  48. cout << "counts<double> ( ) output: \n";
  49. counts<double>();
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 16056KB
stdin
Standard input is empty
stdout
template size: 4; template counts(): 0
10
20
10.5
counts<int>() output:
template size: 4; template counts(): 2
counts<double> ( ) output: 
template size: 8; template counts(): 1