const int not constant expression?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Payne

    const int not constant expression?

    Hello, in my application (which spans approximately 20 source files and a
    2-3 thousand lines) I have a few global varialbes, variables that never
    change their values. One of them is declared like this:
    /* globals.h */
    extern const int g_tray_icon_cal lback;

    /* globals.cpp */
    const int g_tray_icon_cal lback = 4711;

    Now, when I tried to use that variable as a case label in switch statement,
    I got:
    main_dialog_pro cedure.cpp:64: error: case label does not reduce to an
    integer
    constant

    So, I replaced the const int with a #define and it compiles, but why can't I
    use a const int? I thought const would be const, but I guess not...

    // William Payne


  • Andrey Tarasevich

    #2
    Re: const int not constant expression?

    William Payne wrote:[color=blue]
    > Hello, in my application (which spans approximately 20 source files and a
    > 2-3 thousand lines) I have a few global varialbes, variables that never
    > change their values. One of them is declared like this:
    > /* globals.h */
    > extern const int g_tray_icon_cal lback;
    >
    > /* globals.cpp */
    > const int g_tray_icon_cal lback = 4711;
    >
    > Now, when I tried to use that variable as a case label in switch statement,
    > I got:
    > main_dialog_pro cedure.cpp:64: error: case label does not reduce to an
    > integer
    > constant
    >
    > So, I replaced the const int with a #define and it compiles, but why can't I
    > use a const int? I thought const would be const, but I guess not...[/color]

    You can. But in order to be able to use this 'const int' variable in a
    case label, you have to declare it _with_ _initilaizer_ in the same
    translation unit where your 'switch' is located.

    Just put

    const int g_tray_icon_cal lback = 4711;

    into your 'globals.h' file and remove the definition from 'globals.cpp'.

    --
    Best regards,
    Andrey Tarasevich
    Brainbench C and C++ Programming MVP

    Comment

    • ES Kim

      #3
      Re: const int not constant expression?

      "William Payne" <mikas493_no_sp am@student.liu. se> wrote in message
      news:bieaqv$3n0 $1@news.island. liu.se...[color=blue]
      > Hello, in my application (which spans approximately 20 source files and a
      > 2-3 thousand lines) I have a few global varialbes, variables that never
      > change their values. One of them is declared like this:
      > /* globals.h */
      > extern const int g_tray_icon_cal lback;
      >
      > /* globals.cpp */
      > const int g_tray_icon_cal lback = 4711;
      >
      > Now, when I tried to use that variable as a case label in switch statement,
      > I got:
      > main_dialog_pro cedure.cpp:64: error: case label does not reduce to an
      > integer
      > constant
      >
      > So, I replaced the const int with a #define and it compiles, but why can't I
      > use a const int? I thought const would be const, but I guess not...
      >
      > // William Payne
      >
      >[/color]

      It's because g_tray_icon_cal lback defined in globals.cpp has
      internal linkage. const objects have internal linkage by default.
      Andrey provided a solution. Another is to make it have external
      linkage.

      /* globals.cpp */
      extern const int g_tray_icon_cal lback = 4711;

      --
      ES Kim


      Comment

      • William Payne

        #4
        Re: const int not constant expression?


        "Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote in message
        news:vklcik96v0 fr5f@news.super news.com...[color=blue]
        > William Payne wrote:[color=green]
        > > Hello, in my application (which spans approximately 20 source files and[/color][/color]
        a[color=blue][color=green]
        > > 2-3 thousand lines) I have a few global varialbes, variables that never
        > > change their values. One of them is declared like this:
        > > /* globals.h */
        > > extern const int g_tray_icon_cal lback;
        > >
        > > /* globals.cpp */
        > > const int g_tray_icon_cal lback = 4711;
        > >
        > > Now, when I tried to use that variable as a case label in switch[/color][/color]
        statement,[color=blue][color=green]
        > > I got:
        > > main_dialog_pro cedure.cpp:64: error: case label does not reduce to an
        > > integer
        > > constant
        > >
        > > So, I replaced the const int with a #define and it compiles, but why[/color][/color]
        can't I[color=blue][color=green]
        > > use a const int? I thought const would be const, but I guess not...[/color]
        >
        > You can. But in order to be able to use this 'const int' variable in a
        > case label, you have to declare it _with_ _initilaizer_ in the same
        > translation unit where your 'switch' is located.
        >
        > Just put
        >
        > const int g_tray_icon_cal lback = 4711;
        >
        > into your 'globals.h' file and remove the definition from 'globals.cpp'.
        >
        > --
        > Best regards,
        > Andrey Tarasevich
        > Brainbench C and C++ Programming MVP
        >[/color]

        Thank you for the help

        // William Payne


        Comment

        • Andrey Tarasevich

          #5
          Re: const int not constant expression?

          ES Kim wrote:[color=blue]
          > "William Payne" <mikas493_no_sp am@student.liu. se> wrote in message
          > news:bieaqv$3n0 $1@news.island. liu.se...[color=green]
          >> Hello, in my application (which spans approximately 20 source files and a
          >> 2-3 thousand lines) I have a few global varialbes, variables that never
          >> change their values. One of them is declared like this:
          >> /* globals.h */
          >> extern const int g_tray_icon_cal lback;
          >>
          >> /* globals.cpp */
          >> const int g_tray_icon_cal lback = 4711;
          >>
          >> Now, when I tried to use that variable as a case label in switch statement,
          >> I got:
          >> main_dialog_pro cedure.cpp:64: error: case label does not reduce to an
          >> integer
          >> constant
          >>
          >> So, I replaced the const int with a #define and it compiles, but why can't I
          >> use a const int? I thought const would be const, but I guess not...
          >>
          >> // William Payne
          >>
          >>[/color]
          >
          > It's because g_tray_icon_cal lback defined in globals.cpp has
          > internal linkage.[/color]

          That's not true. Provided the declaration of 'g_tray_icon_ca llback' in
          'globals.h' is visible in 'globals.cpp' (i.e. 'globals.h' is included
          into 'globals.cpp'), constant object 'g_tray_icon_ca llback' has external
          linkage since it is declared with 'extern' specifier.
          [color=blue]
          > const objects have internal linkage by default.[/color]

          Yes. But in this case it is not "by default". 'g_tray_icon_ca llback' is
          declared in 'globals.cpp' with explicit 'extern' specifier.
          [color=blue]
          > Andrey provided a solution.[/color]

          My solution actually changes the linkage of 'g_tray_icon_ca llback' to
          internal, meaning that each translation unit will get its own completely
          independent instance of 'g_tray_icon_ca llback'.
          [color=blue]
          > Another is to make it have external
          > linkage.
          >
          > /* globals.cpp */
          > extern const int g_tray_icon_cal lback = 4711;[/color]

          That's not necessary. The declaration in 'globals.h' is sufficient to
          change the linkage of this object.

          The whole point of my solution is to make the initializer ('= 4711')
          visible in all translation units. Your solution doesn't do that, which
          means that it won't solve anything.

          --
          Best regards,
          Andrey Tarasevich
          Brainbench C and C++ Programming MVP

          Comment

          • ES Kim

            #6
            Re: const int not constant expression?

            "Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote in message
            news:dOmdnVrlSY 3Xm9aiXTWJiQ@co mcast.com...

            [snip]

            You're right. Thanks for your concrete explanation.

            --
            ES Kim


            Comment

            Working...