Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: const or constant? |
| Date | 2014-04-24 10:52 +1200 |
| Message-ID | <brquhbF43v1U8@mid.individual.net> (permalink) |
| References | <lj6729$a10$1@speranza.aioe.org> <brqq1bF43v1U6@mid.individual.net> <constexpr-20140423235933@ram.dialup.fu-berlin.de> <brqrqkF43v1U7@mid.individual.net> <constexpr-20140424002650@ram.dialup.fu-berlin.de> |
Stefan Ram wrote:
> Ian Collins <ian-news@hotmail.com> writes:
>> Stefan Ram wrote:
>>> Ian Collins <ian-news@hotmail.com> writes:
>>>>> 1) Declaring that a function will not modify its arguments
>>>>> 2) Declaring that an object resides in ROM (read only memory)
>>>> That's what C++ constexpr supports.
>>> »constexpr« in C++ marks entities whose value is known or
>>> can be calculated at compile time.
>> Correct. It goes further in guaranteeing the value is generated at
>> compile time. It is this guarantee that enables constexpr values to be
>> stored in read only memory.
>
> [OT: this is about "constexpr" in C++]
>
> Some constexpr values are not stored at runtime at all.
>
> For example:
>
> #include <iostream>
> #include <ostream>
>
> constexpr int factorial( int const i )
> { return i > 0 ? i * factorial( i - 1 ): 1; }
>
> int main(){ constexpr int i = factorial( 5 );
> if( i < 5 )::std::cout<< "alpha\n"; }
>
> A C++ compiler with some optimizations enabled
> should compile the above program to the same as:
>
> int main() {}
>
> . There are variables and values in the source code,
> but nothing is stored at runtime.
Well what else would you expect in that example? All you have done with
constexpr is helped the compiler with its optimisation.
If i were required to be visible outside of main, it would have to exist
and would be in read only memory (which might be as a literal constant
the in the executable code).
--
Ian Collins
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread
const or constant? jacob navia <jacob@spamsink.net> - 2014-04-22 18:58 +0200
Re: const or constant? "Bill Cunningham" <nospam@nspam.invalid> - 2014-04-22 14:13 -0400
Re: const or constant? Barry Schwarz <schwarzb@dqel.com> - 2014-04-22 15:59 -0700
Re: const or constant? jt@toerring.de (Jens Thoms Toerring) - 2014-04-22 21:26 +0000
Re: const or constant? James Kuyper <jameskuyper@verizon.net> - 2014-04-22 17:56 -0400
Re: const or constant? Keith Thompson <kst-u@mib.org> - 2014-04-22 15:32 -0700
Re: const or constant? James Kuyper <jameskuyper@verizon.net> - 2014-04-23 08:57 -0400
Re: const or constant? David Brown <david.brown@hesbynett.no> - 2014-04-23 15:27 +0200
Re: const or constant? Keith Thompson <kst-u@mib.org> - 2014-04-23 08:34 -0700
Re: const or constant? David Brown <david.brown@hesbynett.no> - 2014-04-24 08:50 +0200
Re: const or constant? James Kuyper <jameskuyper@verizon.net> - 2014-04-24 07:58 -0400
Re: const or constant? David Brown <david.brown@hesbynett.no> - 2014-04-24 14:19 +0200
Re: const or constant? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-24 21:40 +0000
Re: const or constant? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-04-25 01:05 -0700
Re: const or constant? "BartC" <bc@freeuk.com> - 2014-04-23 21:38 +0100
Re: const or constant? jacob navia <jacob@spamsink.net> - 2014-04-23 00:51 +0200
Re: const or constant? jacob navia <jacob@spamsink.net> - 2014-04-23 00:47 +0200
Re: const or constant? Walter Banks <walter@bytecraft.com> - 2014-04-22 18:00 -0400
Re: const or constant? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-04-22 23:12 +0100
Re: const or constant? jacob navia <jacob@spamsink.net> - 2014-04-23 01:00 +0200
Re: const or constant? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-04-23 14:48 +0100
Re: const or constant? Keith Thompson <kst-u@mib.org> - 2014-04-23 08:40 -0700
Re: const or constant? jacob navia <jacob@spamsink.net> - 2014-04-23 21:49 +0200
Re: const or constant? Kaz Kylheku <kaz@kylheku.com> - 2014-04-23 20:11 +0000
[OT] Killfiles (was Re: const or constant?) Keith Thompson <kst-u@mib.org> - 2014-04-23 14:49 -0700
Re: [OT] Killfiles (was Re: const or constant?) Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-04-23 22:22 -0700
Re: const or constant? James Kuyper <jameskuyper@verizon.net> - 2014-04-23 09:09 -0400
Re: const or constant? Ian Collins <ian-news@hotmail.com> - 2014-04-24 09:35 +1200
Re: const or constant? Ian Collins <ian-news@hotmail.com> - 2014-04-24 10:06 +1200
Re: const or constant? Ian Collins <ian-news@hotmail.com> - 2014-04-24 10:52 +1200
csiph-web