* can one create an array of a size determined by a constant value of a function?
@ 2012-02-04 11:00 Nasser M. Abbasi
2012-02-04 12:03 ` Yannick Duchêne (Hibou57)
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Nasser M. Abbasi @ 2012-02-04 11:00 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: can one create an array of a size determined by a constant value of a function?
2012-02-04 11:00 can one create an array of a size determined by a constant value of a function? Nasser M. Abbasi
@ 2012-02-04 12:03 ` Yannick Duchêne (Hibou57)
2012-02-04 18:36 ` Robert A Duff
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2012-02-04 12:03 UTC (permalink / raw)
Le Sat, 04 Feb 2012 12:00:11 +0100, Nasser M. Abbasi <nma@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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: can one create an array of a size determined by a constant value of a function?
2012-02-04 11:00 can one create an array of a size determined by a constant value of a function? Nasser M. Abbasi
2012-02-04 12:03 ` Yannick Duchêne (Hibou57)
@ 2012-02-04 18:36 ` Robert A Duff
2012-02-04 18:47 ` Jeffrey Carter
2012-02-05 11:12 ` Pascal Obry
3 siblings, 0 replies; 5+ messages in thread
From: Robert A Duff @ 2012-02-04 18:36 UTC (permalink / raw)
"Nasser M. Abbasi" <nma@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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: can one create an array of a size determined by a constant value of a function?
2012-02-04 11:00 can one create an array of a size determined by a constant value of a function? Nasser M. Abbasi
2012-02-04 12:03 ` Yannick Duchêne (Hibou57)
2012-02-04 18:36 ` Robert A Duff
@ 2012-02-04 18:47 ` Jeffrey Carter
2012-02-05 11:12 ` Pascal Obry
3 siblings, 0 replies; 5+ messages in thread
From: Jeffrey Carter @ 2012-02-04 18:47 UTC (permalink / raw)
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 news@netfront.net ---
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: can one create an array of a size determined by a constant value of a function?
2012-02-04 11:00 can one create an array of a size determined by a constant value of a function? Nasser M. Abbasi
` (2 preceding siblings ...)
2012-02-04 18:47 ` Jeffrey Carter
@ 2012-02-05 11:12 ` Pascal Obry
3 siblings, 0 replies; 5+ messages in thread
From: Pascal Obry @ 2012-02-05 11:12 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2012-02-05 11:12 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-02-04 11:00 can one create an array of a size determined by a constant value of a function? Nasser M. Abbasi
2012-02-04 12:03 ` Yannick Duchêne (Hibou57)
2012-02-04 18:36 ` Robert A Duff
2012-02-04 18:47 ` Jeffrey Carter
2012-02-05 11:12 ` Pascal Obry
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox