Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   static const variables in main() (http://www.velocityreviews.com/forums/t283762-static-const-variables-in-main.html)

Michael Klatt 06-16-2004 03:08 PM

static const variables in main()
 
Is there any practical difference between a local variable in main()
declared 'const' and one declared 'static const'?

int main()
{
static const int i1(0);
const int i2(0);
return 0;
}

Gianni Mariani 06-16-2004 03:46 PM

Re: static const variables in main()
 
Michael Klatt wrote:
> Is there any practical difference between a local variable in main()
> declared 'const' and one declared 'static const'?
>
> int main()
> {
> static const int i1(0);
> const int i2(0);
> return 0;
> }


Not with int but for non POD types, the difference is when the
destructor is called.

red floyd 06-16-2004 04:03 PM

Re: static const variables in main()
 
Gianni Mariani wrote:

> Michael Klatt wrote:
>
>> Is there any practical difference between a local variable in main()
>> declared 'const' and one declared 'static const'?
>>
>> int main()
>> {
>> static const int i1(0);
>> const int i2(0);
>> return 0;
>> }

>
>
> Not with int but for non POD types, the difference is when the
> destructor is called.


Wouldn't it make a diff if main() was called recursively?

Alf P. Steinbach 06-16-2004 04:38 PM

Re: static const variables in main()
 
* red floyd:
> Gianni Mariani wrote:
>
> > Michael Klatt wrote:
> >
> >> Is there any practical difference between a local variable in main()
> >> declared 'const' and one declared 'static const'?
> >>
> >> int main()
> >> {
> >> static const int i1(0);
> >> const int i2(0);
> >> return 0;
> >> }

> >
> >
> > Not with int but for non POD types, the difference is when the
> > destructor is called.

>
> Wouldn't it make a diff if main() was called recursively?


Not allowed.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

red floyd 06-16-2004 05:15 PM

Re: static const variables in main()
 
Alf P. Steinbach wrote:
> * red floyd:
>
>>Gianni Mariani wrote:
>>
>>
>>>Michael Klatt wrote:
>>>
>>>
>>>>Is there any practical difference between a local variable in main()
>>>>declared 'const' and one declared 'static const'?
>>>>
>>>>int main()
>>>>{
>>>> static const int i1(0);
>>>> const int i2(0);
>>>> return 0;
>>>>}
>>>
>>>
>>>Not with int but for non POD types, the difference is when the
>>>destructor is called.

>>
>>Wouldn't it make a diff if main() was called recursively?

>
>
> Not allowed.
>


Thanks, Alf... couldn't remember if recursion into main was allowed
(don't have a copy of the standard).

Thomas Matthews 06-16-2004 07:26 PM

Re: static const variables in main()
 
Michael Klatt wrote:
> Is there any practical difference between a local variable in main()
> declared 'const' and one declared 'static const'?
>
> int main()
> {
> static const int i1(0);
> const int i2(0);
> return 0;
> }


In one of my compilers, there was a huge difference between
a static const and a const as far as an array went.

With the static const array declaration, the compiler knows
that there is only one instance and can optimize away. With
the const array declaration, the compiler was actually copying
the const data to a temporary local variable that it created.

Since these are integers, the above point may be moot. Your
best bet is to look at the assembly language generated by
the compiler and see what the difference really is.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


Ronald Landheer-Cieslak 06-16-2004 08:12 PM

Re: static const variables in main()
 
Alf P. Steinbach wrote:

> * red floyd:
>
>>Gianni Mariani wrote:
>>
>>
>>>Michael Klatt wrote:
>>>
>>>
>>>>Is there any practical difference between a local variable in main()
>>>>declared 'const' and one declared 'static const'?
>>>>
>>>>int main()
>>>>{
>>>> static const int i1(0);
>>>> const int i2(0);
>>>> return 0;
>>>>}
>>>
>>>
>>>Not with int but for non POD types, the difference is when the
>>>destructor is called.

>>
>>Wouldn't it make a diff if main() was called recursively?

>
>
> Not allowed.
>

why not?

rlc

Robert Bauck Hamar 06-16-2004 09:01 PM

Re: static const variables in main()
 
* Ronald Landheer-Cieslak wrote:
> Alf P. Steinbach wrote:
>> * red floyd:
>>>Wouldn't it make a diff if main() was called recursively?

>>
>> Not allowed.
>>

> why not?


Because the standard sais so:

From annex C, Clause 3.6:
Main cannot be called recursively and cannot have its address taken
Rationale: The main function may require special acions.
--
Robert Bauck Hamar


All times are GMT. The time now is 05:09 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.