Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. template <typename T> struct Box;
  3. template <typename T, int size> struct Tab;
  4.  
  5. template <typename T>
  6. struct Box {
  7.     T value;
  8.     Box() = default;
  9.     Box(const T& val) : value(val) {}
  10. };
  11.  
  12. template <typename T, int size>
  13. struct Tab {
  14.     T array[size];
  15.    
  16.     Tab() {
  17.         for(int i = 0; i < size; i++)
  18.             array[i] = T(i+1);
  19.     }
  20.  
  21.     T* begin() {
  22.         return array;
  23.     }
  24.  
  25.     T* end() {
  26.         return array + size;
  27.     }
  28. };
  29.  
  30. template<typename T>
  31. std::ostream& operator<< (std::ostream& out, const Box<T>& elem) {
  32.     out << elem.value;
  33.     return out;
  34. }
  35.  
  36. template <typename T>
  37. Box<T> make_Box(const T& val) {
  38.     return Box<T>(val);
  39. }
  40.  
  41. int main() {
  42.     std::cout << Box<int>(23.02) << "\n"; // wypisywany typ to Box<int>
  43.     std::cout << make_Box(23.02) << "\n"; // wypisywany typ to Box<...>
  44.     for(const Box<int>& _ : Tab<Box<int>, 5>())
  45.         std::cout << _ << ", ";
  46. }
  47. /*
  48. 23
  49. 23.02
  50. 1, 2, 3, 4, 5,
  51. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement