0

If I declare an array (using c++ Class Template) like this,

#include <array> //standard c++ library
 int main()
{
 array<int,3> myarray{10,20,30}; 
}

How will I be able to send this array object to a function as a parameter.

If I pass like this it works :

void f_print(array<int, 3> object){}
int main()
{
 array<int, 3> myarray{10,20,30};
 f_print(myarray);
}

but the problem here is I am hard coding the size in the function. The whole point to work like this to use array as an object type so I can use array.size(). So How can I pass this array type object (c++ Class Template) without hardcoding the size.

Edit_1: Why can't I send the array object as only an object where I use all the properties of the object inside the function parameter.

Jabed
  • 178
  • 1
  • 2
  • 13
  • related/dupe: http://stackoverflow.com/questions/17156282/passing-a-stdarray-of-unknown-size-to-a-function. Not the biggest fan of the answers though. – NathanOliver Nov 08 '16 at 15:52
  • Why can't I send the array object as only an object. That's actually my query. – Jabed Nov 08 '16 at 15:59

3 Answers3

5

You can template either the array type

template<typename T>
void f_print(std::array<T, 3> const& object);

Or the size

template<size_t S>
void f_print(std::array<int, S> const& object);

Or both:

template<typename T, size_t S>
void f_print(std::array<T, S> const& object);

The compiler should be able to deduce the correct template arguments in all the cases.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    I think option D: template the parameter to accept a container: `template void f_print const T& object);` would also fit OP's needs – AndyG Nov 08 '16 at 15:58
  • Why can't I send the array object as only an object. That's actually my query – Jabed Nov 08 '16 at 15:59
4

Make the function a template to avoid hard coding it for the size:

template<size_t S>
void f_print(array<int, S> object){}

See a Live Demo.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Why can't I send the array object as only an object. That's actually my query – Jabed Nov 08 '16 at 15:59
  • 3
    @JabedHossain Because each of the template parameter combinations of `std::array` forms it's own class. These aren't really related and cannot be deduced to _the_ array object. Also if that is your real question, why don't you put that in your question post? – πάντα ῥεῖ Nov 08 '16 at 16:02
1

I think you are missing the point of arrays. It's in the very nature of an array that its size belongs to its type. That's the case for raw arrays and it's the case for std::array as well. Hard-coding the size 3 of std::array<int, 3> in the function is as unavoidable as hard-coding the int.

In fact, in my experience, the best way to think about an array like for example int[4] or std::array<int, 4> is as a shorthand for int a, b, c, d.

I'd say a template only circumvents the issue in a very superficial way. If you create a template with a size parameter, then you only end up with a lot of different functions for different array types. std::array<int, 3>, std::array<int, 4>, std::array<int, 5> and so on all result in different template instantiations. It's in the same way as you end up with different template instantiations for std::array<int, 3>, std::array<double, 3> and std::array<std::string, 3>.

Mind that turning the size into a template parameter may still be the solution you are actually looking for. If, however, you need a container for which the size is not known at compile time, then use std::vector.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
  • I knew I could use std::vector but I wanted to know as I am declaring class type object array why the array doesn't behave like an object while its being used as a function parameter. But thanks for the answer. Learned from it . – Jabed Nov 08 '16 at 17:21