Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can one create an array of a size determined by a constant value of a function?

7 views
Skip to first unread message

Nasser M. Abbasi

unread,
Feb 4, 2012, 6:00:11 AM2/4/12
to
Quick question for the experts:

I was looking at c++11 standard, where it show how
one can allocate an array of some size. The size is
determined by a function call. This is done at compile
time though where the compiler can determine the result
of the function, like this:

http://en.wikipedia.org/wiki/C%2B%2B11#cite_note-2

"constexpr int get_five() {return 5;}
int some_value[get_five() + 7]; // Create an array of 12 integers. Legal C++11
This allows the compiler to understand, and verify, that get_five is
a compile-time constant.
"

constexpr is new and was added in c++11

Is it possible to do something like this in Ada?
I am not sure now how useful or common such a feature would
be actually, but was just wondering how it will look
in Ada.

thanks,

--Nasser

Yannick Duchêne (Hibou57)

unread,
Feb 4, 2012, 7:03:04 AM2/4/12
to
Le Sat, 04 Feb 2012 12:00:11 +0100, Nasser M. Abbasi <n...@12000.org> a
écrit:

> Quick question for the experts:
>
> I was looking at c++11 standard, where it show how
> one can allocate an array of some size. The size is
> determined by a function call. This is done at compile
> time though where the compiler can determine the result
> of the function, like this:
>
> http://en.wikipedia.org/wiki/C%2B%2B11#cite_note-2
>
> "constexpr int get_five() {return 5;}

Sba7 nnour Nasser :-P

Just my own opinion (others may have another).

With C++, “get_five()” desigate a function call, while “get_five”
designate an object (well, if it's a macro, it designate who‑know‑what).

With Ada Get_Five may designate both a function call or an entity.

If I understood your matter, conclusion is: Ada has this from its very
beginning.

Or are you seeking for expression functions introduced since Ada 2012 ?
If so, have a look here then:
http://www.ada-auth.org/standards/12rm/html/RM-6-8.html

--
“Syntactic sugar causes cancer of the semi-colons.” [1]
“Structured Programming supports the law of the excluded muddle.” [1]
[1]: Epigrams on Programming — Alan J. — P. Yale University

Robert A Duff

unread,
Feb 4, 2012, 1:36:35 PM2/4/12
to
"Nasser M. Abbasi" <n...@12000.org> writes:

> I was looking at c++11 standard, where it show how
> one can allocate an array of some size. The size is
> determined by a function call. This is done at compile
> time though where the compiler can determine the result
> of the function, like this:
>
> http://en.wikipedia.org/wiki/C%2B%2B11#cite_note-2
>
> "constexpr int get_five() {return 5;}
> int some_value[get_five() + 7]; // Create an array of 12 integers. Legal C++11
> This allows the compiler to understand, and verify, that get_five is
> a compile-time constant.
> "

You can't declare that a function will return a compile-time-known
value. But if the goal is efficiency, you don't need to -- the
compiler can figure it out. For example, compile the program
below with "gcc -O2", and look at the generated assembly.
You'll see that Get_Five has vanished, and the array is allocated
just as if you had said "1..12" -- the expression "Get_Five+7" is
evaluated at compile time.

If Get_Five were separately compiled, you might need pragma
Inline.

If Get_Five returned something not known at compile time,
then the calculation would be done at run time, and a dynamic
array would be allocated on the stack.

with Text_IO; use Text_IO;
procedure Main is

function Get_Five return Integer;
function Get_Five return Integer is
begin
return 5;
end Get_Five;

Some_Value: array (Integer range 1 .. Get_Five + 7) of Integer;

begin
for X in Some_Value'Range loop
Some_Value(X) := X;
end loop;

Put_Line(Some_Value(12)'Img);
end Main;

- Bob

Jeffrey Carter

unread,
Feb 4, 2012, 1:47:29 PM2/4/12
to
On 02/04/2012 04:00 AM, Nasser M. Abbasi wrote:

> "constexpr int get_five() {return 5;}
> int some_value[get_five() + 7]; // Create an array of 12 integers. Legal C++11
> This allows the compiler to understand, and verify, that get_five is
> a compile-time constant.
> "

With Ada, you can declare an array with a size not known at compile time:

function Get return Positive is
-- null;
begin -- Get
return Integer (Ada.Calendar.Seconds (Ada.Calendar.Clock) );
end Get;

S : String (1 .. Get);

--
Jeff Carter
"Nobody expects the Spanish Inquisition!"
Monty Python's Flying Circus
22

--- Posted via news://freenews.netfront.net/ - Complaints to ne...@netfront.net ---

Pascal Obry

unread,
Feb 5, 2012, 6:12:09 AM2/5/12
to
Le 04/02/2012 12:00, Nasser M. Abbasi a écrit :
> Is it possible to do something like this in Ada?

Yes, easier and better and since 1983 :)

subtype Size is Positive range 1 .. 1_024;

function Spaces (N : in Size) return String;
-- Returns a string with N spaces

function Spaces (N : in Size) return String is
V : String (1 .. N) := (others => ' ');
begin
return V;
end Spaces;

N is not known at compile time and Spaces can be called by any other
part of the application with any value of N.

Note that in the above routine I have declared V to make it clear that
an array with a variable length can be declared, but we can simplify
this by writing:

function Spaces (N : in Size) return String is
begin
return String'(1 .. N => ' ');
end Spaces;

Pascal.
PS: all this code has not been compiled!

--

--|------------------------------------------------------
--| Pascal Obry Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://www.obry.net - http://v2p.fr.eu.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver keys.gnupg.net --recv-key F949BD3B

0 new messages