6
votes

In C++ is possible to pass to a function a vector by const reference in this way:

void test(const std::vector<int>& a);

This is useful when the vector is very big and I want to avoid the waste of time of making copy of it. I know that the same code in Delphi is:

procedure test(const [Ref] a: array of Integer);

Does it also have the same effect as C++ (pass the address instead of a copy and optimize/save time)? Is this the only way or there is also something else to optimize the parameter passing?

2
You are confusing open array parameters and dynamic arrays. There is no need for the [Ref] here. Open array parameters are passed as two parameters. The first one contains the address of the first element of the array. The second one the length of the array (numer of elements) minus one, the so called High() value. - Rudy Velthuis
Delphi dynamic arrays are always reference-types. But you should use dynamic arrays, not open arrays (it is a legacy-derived mess here, I admit). For example procedure test(const a: TArray<integer>); - Arioch 'The
@Arioch'The That prevents you from passing statically allocated arrays, or using open array constructors. It forces you into heap allocation where none is needed. Open array parameters are not legacy, they are mainstream, well designed, and very useful. They should be used where possible to make your code as composable as possible. - David Heffernan
I don't see how static arrays are anything like std::vector. Come one, you even suggested using TList instead - how about statically allocated TList instances ??? - Arioch 'The
@Arioch'The I did not suggest anything at all, read my answer more carefully please. I attempted to present facts. Looking purely at Delphi code, open arrays parameters are more flexible than dynamic parameters, certainly for input. That is inarguable fact. The only point where open array parameters fall down is if you wish the callee to modify the length of the array. In all other circumstances there are advantages to using open array parameters. - David Heffernan

2 Answers

13
votes
procedure test(const a: array of Integer);

This is an open array, passed as const. These are already passed by reference. Adding [ref] in this situation is needless.

Only if you pass an open array by value will a copy will be made:

procedure test(a: array of Integer);

The other option, for sake of completeness, is to pass by var.

procedure test(var a: array of Integer);

Here the array is passed by reference, but, in contrast to the const array, the compiler permits its content to be modified.


I know that the same code in Delphi is ...

That's not quite accurate. Probably the best mapping from C++ std::vector<T> is to Delphi's TList<T>. Probably the closest match to a Delphi open array parameter would be a C++ array parameter. You might map your Delphi procedure:

procedure test(const a: array of Integer);

to this C++ function:

void test(const int a[], const size_t len);

So you aren't really comparing like for like.

That said, Delphi dynamic arrays, which you might well be using when you actually call such a function, are managed types. This means that their lifetimes are managed by automatic reference counting (ARC) and that sets them apart from raw C++ arrays.

I'm rambling somewhat now. Mostly what I am trying to get at is that the devil is in the details. None of these things map perfectly between these languages because the languages have their subtle differences.

But, leaving these nuances aside, if you wish to pass an array efficiently in Delphi, then a const open array will achieve that.

7
votes

You are confusing open array parameters and dynamic arrays. There is no need for the [Ref] here.

Open array parameters are actually passed as two parameters.

  • The first one contains the address of the first element of the array.
  • The second one the length of the array (number of elements) minus one, the so called High() value.

A vector in C++ is a class. It is passed like a class in Delphi, but constness is different. In Delphi, even if you pass a class as const, its methods can still be called. And in Delphi, class instances are references already. No need for the [Ref].

More info on open array parameters in my article.