ThePunisher
ThePunisher

Reputation: 216

Define a c++ array as int[int] array(5);

I was experimenting with FreeFem++, which is basically a c++ compiler with some added libraries.

As you can see here: https://doc.freefem.org/references/types.html a 5-element array can be defined and printed as:

int n = 5;
real[int] Ai(n);
for (int i = 0; i < n; i++)
    Ai[i] = i;
cout << Ai << endl;

(note that real is a custom type which is in fact a double).

As a (novice) c++ developer, I would have defined the class real using templates, and with the proper constructor I would have been able to do the same with some call like real<int>.

Question

is it possible to write a c++ library to make the above FreeFem++ code work (specifically, I want squared [] brackets, not <>)?

My attempt

Being FreeFem++ open source, I tried to have a look a t the code.

https://github.com/FreeFem/FreeFem-sources/blob/master/src/fflib/array_real.cpp

I can not really decipher it, however my guess is that it is not possible, and in fact is the FreeFem++ compiler that swaps the [int] with some <int>.

Bonus

Sometimes I also see parts of code like

cout << v[] << endl;

where it prints a vector, however to access elements of the vector I have to do v[][5]. What kind of sorcery is that? Overloading operator[](void) would never work, right?

Upvotes: 0

Views: 129

Answers (0)

Related Questions