How is "static const int" better than "static enum"?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ajax Chelsea

    How is "static const int" better than "static enum"?

    can not the "static const int" be replaced by "static enum" anywhere?

    is it necessary that define special initialization syntax for "static const int"?
  • Phlip

    #2
    Re: How is "static const int" better than "static enum"?

    Ajax Chelsea wrote:
    [color=blue]
    > can not the "static const int" be replaced by "static enum" anywhere?[/color]

    enum is a type, not a variable, so it needs no 'static' storage category.

    'int' has an implementation-defined size, and its type is compatible with
    variable ints.

    'enum' is only guaranteed to have enough bits to store any value used in
    their definition.

    For a while, compilers could not treat 'static const int' inside a class as
    a compile-time constant, and so one couldn't size arrays with it and such.
    Using 'enum' as a scalar instead of a typed flag was an easy work-around.
    [color=blue]
    > is it necessary that define special initialization syntax for "static[/color]
    const int"?

    ?

    Constant static data are the only things that can declare inside a class.
    This (I suspect) grants them their compile-time constant status.

    This is all well-formed, with defined behavior:

    class yo { public:
    static int z (42);
    };

    char whatever[yo::z];

    But an enum would have worked the same, too.

    --
    Phlip


    Comment

    • Rob Williscroft

      #3
      Re: How is "static const int" better than "static enum"?

      Phlip wrote in news:eyfzb.3350 3$nj7.10044@new ssvr32.news.pro digy.com:
      [color=blue]
      > This is all well-formed, with defined behavior:
      >
      > class yo { public:
      > static int z (42);
      > };
      >
      > char whatever[yo::z];
      >
      > But an enum would have worked the same, too.
      >
      >[/color]

      Also if you also want to use the static integral constant in a
      non-compile-time context you also need a definition outside the class,

      /* Not in a header file (templates aside)
      */
      int yo::z; /* Note no initializer */

      int main()
      {
      int z = yo::z;
      int const *zp = &yo::x;
      }

      enum's don't have this requirment, which is perhapse one way in which
      enum's are "better" than static int const's.

      Cranking the level of triviality up a notch. An instance of an enum
      can also be a static integral constant,

      #include <iostream>

      struct A
      {
      enum B { C, D, E };
      static B const b = A::E;
      };

      A::B const A::b;

      int main()
      {
      std::cerr << A::b << "\n";
      }

      Rob.
      --

      Comment

      • Ajax Chelsea

        #4
        Re: How is &quot;static const int&quot; better than &quot;static enum&quot;?

        Rob Williscroft <rtw@freenet.RE MOVE.co.uk> wrote in message news:<Xns944677 5E0B83CukcoREMO VEfreenetrtw@19 5.129.110.204>. ..[color=blue]
        >
        > Cranking the level of triviality up a notch. An instance of an enum
        > can also be a static integral constant,
        >
        > #include <iostream>
        >
        > struct A
        > {
        > enum B { C, D, E };
        > static B const b = A::E;
        > };
        >
        > A::B const A::b;
        >
        > int main()
        > {
        > std::cerr << A::b << "\n";
        > }
        >
        > Rob.[/color]

        so I consider that it is not necessary to specialize syntax of "static
        const int(long...)", haha

        Comment

        Working...