6

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?

12
  • 1
    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. Commented Dec 15, 2016 at 9:00
  • 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>); Commented Dec 15, 2016 at 9:15
  • 1
    @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. Commented Dec 15, 2016 at 10:03
  • 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 ??? Commented Dec 15, 2016 at 10:43
  • @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. Commented Dec 15, 2016 at 10:47

2 Answers 2

14
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.

Sign up to request clarification or add additional context in comments.

Comments

8

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.

6 Comments

In the C++ code, you can call methods on the vector instance, but only the const methods.
Ok but if I passed an open array as const [Ref] does it make sense? Or they are passed by ref by default?
@Rudy I don't see any evidence of confusion with dynamic arrays. The question does not mention dynamic arrays.
@RaffaeleRossi technically, open arrays are TWO values rather than one. And they are not the initial source some-array-kind variable but rather a result of implicit types-casting function over those docwiki.embarcadero.com/RADStudio/Berlin/en/Open_Arrays
@David: He mentions std::vector, which is more or less the C++ (class) equivalent of a dynamic array (or, actually, of a TList<T>). FWIW, I didn't say that in C++, you could call methods other than const. But in Delphi, you can.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.