sizeof in preprocessor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    sizeof in preprocessor

    How can I write this correctly?

    #if sizeof(int) < sizeof(my_int)
    ..............
    #endif

    I want compile different pieces of code for different platforms


  • Nick Hounsome

    #2
    Re: sizeof in preprocessor


    "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> wrote in message
    news:c4lv0p$bbl $1@nic.grnet.gr ...[color=blue]
    > How can I write this correctly?
    >
    > #if sizeof(int) < sizeof(my_int)
    > .............
    > #endif
    >
    > I want compile different pieces of code for different platforms
    >
    >[/color]

    You can't in the preprocessor but you could use
    specialization of templates based on sizeof



    Comment

    • Nick Hounsome

      #3
      Re: sizeof in preprocessor


      "<- Chameleon ->" <cham_gss@hotma il.NOSPAM.com> wrote in message
      news:c4lv0p$bbl $1@nic.grnet.gr ...[color=blue]
      > How can I write this correctly?
      >
      > #if sizeof(int) < sizeof(my_int)
      > .............
      > #endif
      >
      > I want compile different pieces of code for different platforms
      >
      >[/color]

      You can't in the preprocessor but you could use
      specialization of templates based on sizeof



      Comment

      • Rolf Magnus

        #4
        Re: sizeof in preprocessor

        - Chameleon - wrote:
        [color=blue]
        > How can I write this correctly?
        >
        > #if sizeof(int) < sizeof(my_int)
        > .............
        > #endif[/color]

        You can't. the value of sizeof() is not evaluated by the preprocessor.
        Therefore, you can't use it in preprocessor conditionals. You could do:

        #include <climits>
        #define MY_INT_MAX some_value
        #if INT_MAX < MY_INT_MAX
        //something
        #else
        //something else
        #endif

        or you use templates: (untested)

        template <bool b>
        void my_function_int ernal()
        {
        //something
        }

        template <>
        void my_function_int ernal<false>()
        {
        //something else
        }

        void my_function()
        {
        my_function_int ernal<(sizeof(i nt) < sizeof(my_int)) >();
        }

        Comment

        • Rolf Magnus

          #5
          Re: sizeof in preprocessor

          - Chameleon - wrote:
          [color=blue]
          > How can I write this correctly?
          >
          > #if sizeof(int) < sizeof(my_int)
          > .............
          > #endif[/color]

          You can't. the value of sizeof() is not evaluated by the preprocessor.
          Therefore, you can't use it in preprocessor conditionals. You could do:

          #include <climits>
          #define MY_INT_MAX some_value
          #if INT_MAX < MY_INT_MAX
          //something
          #else
          //something else
          #endif

          or you use templates: (untested)

          template <bool b>
          void my_function_int ernal()
          {
          //something
          }

          template <>
          void my_function_int ernal<false>()
          {
          //something else
          }

          void my_function()
          {
          my_function_int ernal<(sizeof(i nt) < sizeof(my_int)) >();
          }

          Comment

          • Jeff Schwab

            #6
            [OT] Re: sizeof in preprocessor

            <- Chameleon -> wrote:[color=blue]
            > How can I write this correctly?
            >
            > #if sizeof(int) < sizeof(my_int)
            > .............
            > #endif
            >
            > I want compile different pieces of code for different platforms
            >
            >[/color]

            I'd love to answer this for you, but since I'm Jewish, I assume you
            don't want my help.


            Comment

            • Jeff Schwab

              #7
              [OT] Re: sizeof in preprocessor

              <- Chameleon -> wrote:[color=blue]
              > How can I write this correctly?
              >
              > #if sizeof(int) < sizeof(my_int)
              > .............
              > #endif
              >
              > I want compile different pieces of code for different platforms
              >
              >[/color]

              I'd love to answer this for you, but since I'm Jewish, I assume you
              don't want my help.


              Comment

              • Guest's Avatar

                #8
                Re: [OT] Re: sizeof in preprocessor

                > > How can I write this correctly?[color=blue][color=green]
                > >
                > > #if sizeof(int) < sizeof(my_int)
                > > .............
                > > #endif
                > >
                > > I want compile different pieces of code for different platforms
                > >
                > >[/color]
                >
                > I'd love to answer this for you, but since I'm Jewish, I assume you
                > don't want my help.[/color]

                your answer is off-topic


                Comment

                • Guest's Avatar

                  #9
                  Re: [OT] Re: sizeof in preprocessor

                  > > How can I write this correctly?[color=blue][color=green]
                  > >
                  > > #if sizeof(int) < sizeof(my_int)
                  > > .............
                  > > #endif
                  > >
                  > > I want compile different pieces of code for different platforms
                  > >
                  > >[/color]
                  >
                  > I'd love to answer this for you, but since I'm Jewish, I assume you
                  > don't want my help.[/color]

                  your answer is off-topic


                  Comment

                  • Jeff Schwab

                    #10
                    Re: [OT] Re: sizeof in preprocessor

                    <- Chameleon -> wrote:[color=blue][color=green][color=darkred]
                    >>>How can I write this correctly?
                    >>>
                    >>>#if sizeof(int) < sizeof(my_int)
                    >>>............ .
                    >>>#endif
                    >>>
                    >>>I want compile different pieces of code for different platforms
                    >>>
                    >>>[/color]
                    >>
                    >>I'd love to answer this for you, but since I'm Jewish, I assume you
                    >>don't want my help.[/color]
                    >
                    >
                    > your answer is off-topic
                    >
                    >[/color]

                    Yes, that's what [OT] in the subject line means.

                    Comment

                    • Jeff Schwab

                      #11
                      Re: [OT] Re: sizeof in preprocessor

                      <- Chameleon -> wrote:[color=blue][color=green][color=darkred]
                      >>>How can I write this correctly?
                      >>>
                      >>>#if sizeof(int) < sizeof(my_int)
                      >>>............ .
                      >>>#endif
                      >>>
                      >>>I want compile different pieces of code for different platforms
                      >>>
                      >>>[/color]
                      >>
                      >>I'd love to answer this for you, but since I'm Jewish, I assume you
                      >>don't want my help.[/color]
                      >
                      >
                      > your answer is off-topic
                      >
                      >[/color]

                      Yes, that's what [OT] in the subject line means.

                      Comment

                      Working...