Advertisement
Ginsutime

Array Cherno 2

Feb 20th, 2022
1,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <array>
  3.  
  4. template<typename T, size_t S> // Makes it so it's not specific to integers
  5. class Array
  6. {
  7. public:
  8.     constexpr int Size() const { return S; } // Function's sole job is to return constant value of whatever S is set to
  9. private:
  10.     T m_Data[S]; // Replaced int m_Data[S];
  11. };
  12.  
  13. int main()
  14. {
  15.     int size = 5;
  16.     Array<int, 5> data; // Version of this class where T is set to int and size set to 5
  17.  
  18.     static_assert(data.Size() < 10, "Size is too large!"); // int Size() needs to be constexpr so it can be evaluated at compile time
  19.                                                            // If not, data from data.Size() will be an error since it must have a const value
  20.     Array<std::string, data.Size()> newArray;
  21.  
  22.     for (int i = 0; i < data.Size(); i++)
  23.     {
  24.  
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement