Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #define USE_CPP
  4. #ifdef USE_CPP
  5. //CPP code
  6. template<typename T_Value>
  7. struct Span
  8. {
  9.     template<int T_ArraySize>
  10.     Span(T_Value (&_value)[T_ArraySize])
  11.     {
  12.         Pointer = &_value[0];
  13.         Size = T_ArraySize;
  14.     }
  15.     Span(std::vector<T_Value>& _value)
  16.     {
  17.         Pointer = &_value[0];
  18.         Size = _value.size();
  19.     }
  20.     int size() const { return Size; }
  21.     T_Value operator[](int _Index) { return Pointer[_Index]; }
  22.   public:
  23.     T_Value* Pointer;
  24.     int Size;
  25. };
  26.  
  27. inline void CallFunctionThatTakesArrayWithAnySize(Span<int> _array)
  28. {
  29.     for (int i = 0; i < _array.size(); ++i)
  30.     {
  31.         printf("%d", _array[i]);
  32.     }
  33. }
  34. int main()
  35. {
  36.     int AnArray[5];
  37.     CallFunctionThatTakesArrayWithAnySize(AnArray);
  38. }
  39. #else
  40. //C code
  41. inline void CallFunctionThatTakesArrayWithAnySize(int* _array, int _arraysize)
  42. {
  43.     for (int i = 0; i < _arraysize; ++i)
  44.     {
  45.         printf("%d", _array[i]);
  46.     }
  47. }
  48. int main()
  49. {
  50.     int AnArray[5];
  51.     CallFunctionThatTakesArrayWithAnySize(AnArray, 5);
  52. }
  53.  
  54. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement