Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static const int problem

Reply
Thread Tools

static const int problem

 
 
Öö Tiib
Guest
Posts: n/a
 
      07-28-2013
On Sunday, 28 July 2013 19:32:40 UTC+3, Paavo Helde wrote:
> Chris Vine <(E-Mail Removed)> wrote in
> > Generally I prefer const static integral members (or constexpr static
> > integral members) to enum, notwithstanding these occasional oddities.

>
> And the reason is...?


Enumerator may cause inconvenience in C++ when such constant value
should be of particular integral type.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-28-2013
Paavo Helde wrote:
> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in
> news:(E-Mail Removed):


>> class C {
>> private:
>> static const int m_static = 2;

>
> Yes, this is a declaration which will need a definition in some source
> file somewhere. If there is no such definition, the linker may complain
> or not, depending on the usage of this symbol and optimization levels,
> and obviously depending on which compiler you use.
>
> Unfortunately, at least some MSVC versions fail to compile the correct
> code with the definition added, complaining about multiply defined
> symbols. So to avoid all the hassle I would suggest just to use:
>
> class C {
> private:
> enum constants {
> m_static = 2
> };


Which compilers? I haven't had to resort to that kludge for 20 years or
so...

--
Ian Collins
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-29-2013
Paavo Helde wrote:
> Ian Collins <(E-Mail Removed)> wrote in news:b5lga3Fmlo1U2
> @mid.individual.net:
>
>> Paavo Helde wrote:
>>> (E-Mail Removed) wrote in
>>> news:(E-Mail Removed):

>>
>>>> class C {
>>>> private:
>>>> static const int m_static = 2;
>>>
>>> Yes, this is a declaration which will need a definition in some source
>>> file somewhere. If there is no such definition, the linker may

> complain
>>> or not, depending on the usage of this symbol and optimization levels,
>>> and obviously depending on which compiler you use.
>>>
>>> Unfortunately, at least some MSVC versions fail to compile the correct
>>> code with the definition added, complaining about multiply defined
>>> symbols. So to avoid all the hassle I would suggest just to use:
>>>
>>> class C {
>>> private:
>>> enum constants {
>>> m_static = 2
>>> };

>>
>> Which compilers?

>
> MSVC++ 2010 and probably all earlier versions, have not tried with later
> ones.
>
>> I haven't had to resort to that kludge for 20 years or
>> so...

>
> What kludge? To my mind, a const static integral member is a huge kludge.
> It is like a Cheshire/Bohr half-existing cat, at one moment it is a real
> object occupying a real memory region, on the next it is not. It appears
> C++11 has made the situation worse with the ODR-use thing, making it
> virtually impossible to predict if such a thing should be defined in a
> compilation unit in addition to a declaration or not.


Looking my code, nearly all const static integral members are private
sizes, limits (buffer sizes mainly) and IP port numbers which never have
their address evaluated. If something is a size, I prefer to declare it
as a size_t. At least one example I found was > INT_MAX, which I guess
would be undefined as an enum value.

--
Ian Collins
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      07-29-2013
On Sunday, 28 July 2013 20:34:34 UTC+3, Paavo Helde wrote:
> Öö Tiib <(E-Mail Removed)> wrote in news:8331b7c9-c009-4095-ac0b-
> (E-Mail Removed):
> > On Sunday, 28 July 2013 19:32:40 UTC+3, Paavo Helde wrote:
> >> Chris Vine <(E-Mail Removed)> wrote in
> >> > Generally I prefer const static integral members (or constexpr static
> >> > integral members) to enum, notwithstanding these occasional oddities..
> >>
> >> And the reason is...?

> >
> > Enumerator may cause inconvenience in C++ when such constant value
> > should be of particular integral type.

>
> I guess that's why C++11 introduced strongly-typed enums.


The underlying type of C++11 fixes the issues caused by signedness and
byte size of enumerator. That is lot better, but type may still be needed
for other reasons, for example for overload resolution.

4.5/4: "A prvalue of an unscoped enumeration type whose underlying type
is fixed (7.2) can be converted to a prvalue of its underlying type.
Moreover, if integral promotion can be applied to its underlying type,
a prvalue of an unscoped enumeration type whose underlying type is
fixed can also be converted to a prvalue of the promoted underlying
type."

I can't find where it defines any rank differences to those conversions.
That seems to make it ambiguous what overload to choose. No such problem
with 'constexpr char' for example for what overload that expects char is
exact match.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-29-2013
Paavo Helde wrote:
> Ian Collins <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
>>
>> Looking my code, nearly all const static integral members are private
>> sizes, limits (buffer sizes mainly) and IP port numbers which never
>> have their address evaluated.

>
> And this is ruined by passing it to an innocent-looking std::max() or
> adding to an iterator.
>
>> If something is a size, I prefer to
>> declare it as a size_t.
>> At least one example I found was > INT_MAX,
>> which I guess would be undefined as an enum value.

>
> enum constants: size_t { kMaxSize = size_t(-1) };


I'm talking code written over many years.

--
Ian Collins
 
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 int" versus "const int" John Goche C++ 4 11-11-2011 07:41 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 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
unsigned int const does not match const unsigned int Timo Freiberger C++ 3 10-30-2004 07:02 PM



Advertisments