Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static const variables in main()

Reply
Thread Tools

static const variables in main()

 
 
Michael Klatt
Guest
Posts: n/a
 
      06-16-2004
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;
}
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      06-16-2004
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.
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      06-16-2004
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?
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      06-16-2004
* 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?
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      06-16-2004
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).
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      06-16-2004
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

 
Reply With Quote
 
Ronald Landheer-Cieslak
Guest
Posts: n/a
 
      06-16-2004
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
 
Reply With Quote
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      06-16-2004
* 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
static const T vs static T const er C++ 3 04-22-2008 10:22 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
const static Vs. static const Dave C++ 10 05-22-2005 10:32 PM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM



Advertisments