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

Discussion in 'C++' started by Ajax Chelsea, Dec 3, 2003.

  1. Ajax Chelsea

    Ajax Chelsea Guest

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

    is it necessary that define special initialization syntax for "static const int"?
     
    Ajax Chelsea, Dec 3, 2003
    #1
    1. Advertising

  2. Ajax Chelsea

    Phlip Guest

    Ajax Chelsea wrote:

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


    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.

    > is it necessary that define special initialization syntax for "static

    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
     
    Phlip, Dec 3, 2003
    #2
    1. Advertising

  3. Phlip wrote in news:eyfzb.33503$:

    > 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.
    >
    >


    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.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Dec 3, 2003
    #3
  4. Ajax Chelsea

    Ajax Chelsea Guest

    Rob Williscroft <> wrote in message news:<Xns9446775E0B83CukcoREMOVEfreenetrtw@195.129.110.204>...
    >
    > 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.


    so I consider that it is not necessary to specialize syntax of "static
    const int(long...)", haha
     
    Ajax Chelsea, Dec 5, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    14
    Views:
    804
    Alexander Terekhov
    Sep 22, 2003
  2. Guest
    Replies:
    3
    Views:
    521
    Jonathan Turkanis
    Jan 24, 2004
  3. Peter Bencsik
    Replies:
    2
    Views:
    878
  4. John Goche
    Replies:
    4
    Views:
    641
    Victor Bazarov
    Nov 11, 2011
  5. Replies:
    2
    Views:
    69
    Mark H Harris
    May 13, 2014
Loading...

Share This Page