How to pass a dynamic object of the type array to a function?

  • A+
Category:Languages

I want to pass a dynamic size standard and of typename type array to a function.

I can't figure out how to do it. Why can't I just accept a reference to the object array?

The code I tried:

#include <iostream> #include <array>  using namespace std;  template <typename T> void showArrays(void *myArrayPointer, int size, T type) {     array<T, size> myArray = myArrayPointer;     for (int i = 0; i < size; i++) {         cout << myArray.at(i) << " /n";     } }  int main() {     array<int,6> myArray = { 1,2,3,4,5,6 };     cout << "The array is /n";     showArrays(&myArray,6,0);      return 0; } 

But I get expected compile-time constant expression for the Size still. My function header is also not very pretty. But I couldn't figure out a way to have the size dynamic without passing a generic pointer or creating a template of the class array where the size is an attribute.

 


If you want a function template where the type is a template parameter, but the size is a runtime property (which could make sense if you want to avoid binary bloat), then you want something like this:

template <typename T> void showArrays(T* p, int n) {     for (int i = 0; i < n; ++i) {         std::cout << p[i] << '/n';     } }  int main() {     std::array<int, 6> myArray = { 1,2,3,4,5,6 };     std::cout << "The array is /n";     showArrays(myArray.data(), 6);  // or use myArray.size() } 

You can reuse your template for other kinds of contiguous arrays, too:

float a[] = { 1.1, 2.2, 3.3, 4.4 }; showArrays(a, 4);        // full array showArrays(a + 1, 2);    // just the middle two  std::vector<long> v = /* ... */; showArrays(v.data(), v.size());  std::string s = "hello world"; showArrays(s.data() + 6, 5); 

Note that T is a template parameter and not a function parameter. Note further that we never specify an argument for the parameter: that's because the argument is deduced. If you did want to pass a void pointer, like you did in your example, then you would not be able to deduce the template argument from the function call, and you'd have to specify it explicitly:

template <typename T> void showArrays(void* p, int n) { //              ^^^^^     for (int i = 0; i < n; ++i) {         std::cout << static_cast<T*>(p)[i] << '/n';         //           ^^^^^^^^^^^^^^^^^^         //           cast to object pointer,         //           note that "T" shows up in your code now!     } }  showArrays<int>(myArray.data(), myArray.size()); //        ^^^^^ //        explicit template argument 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: