Advertisement
Guest User

Untitled

a guest
Dec 9th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <xutility>
  2. #include <iostream>
  3.  
  4. template <typename Ty>
  5. struct Simple_Array
  6. {
  7.     Ty* data;
  8.     const size_t size;
  9.  
  10.     Simple_Array(size_t size) :
  11.         size(size),
  12.         data(new Ty[size])
  13.     {
  14.     }
  15.  
  16. //  Throws an exception.
  17.     template <class It>
  18.     Simple_Array(It first, It last) :
  19.         size(std::distance(first, last)),
  20.         data(new Ty[size])
  21.     {
  22.         for (int i = 0; first != last; ++first, ++i)
  23.             data[i] = *first;
  24.     }
  25.  
  26. //  This version works without any problems.
  27. /*  template <class It>
  28.     Simple_Array(It first, It last) :
  29.         Simple_Array(std::distance(first, last))
  30.     {
  31.         for (int i = 0; first != last; ++first, ++i)
  32.             data[i] = *first;
  33.     }*/
  34. };
  35.  
  36. int main() {
  37.     auto list = { 1, 2, 3, 4 };
  38.  
  39.     try {
  40.         Simple_Array<int>(list.begin(), list.end());
  41.     }
  42.     catch (...) {
  43.         std::cout << "Failed." << std::endl;
  44.     }
  45.  
  46.     std::cin.get();
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement