#define vs. const

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicolas Pavlidis

    #16
    Re: #define vs. const

    Ioannis Vranos <ivr@guesswh.at .grad.com> writes:
    [color=blue]
    > Nicolas Pavlidis wrote:
    >[color=green]
    > > You mean the assert - macro defined in cassert, what Chris means is
    > > something like boost::STATIC_A SSERT, which makes the ckeck at
    > > compiletime.[/color]
    >
    >
    > How could this be possible for non compile-time constants?[/color]

    It's thought to be used only for things that are clear at compiletime.
    For example if you're writing templates and becaus of the semantics you
    have to reduce the possible types which can be passed to this template,
    or if ranges were passed to a template via constants and there is a
    upper and lower bound for this constants.

    Best regrads,
    Nicolas
    --
    | Nicolas Pavlidis | Elvis Presly: |\ |__ |
    | Student of SE & KM | "Into the goto" | \|__| |
    | pavnic@sbox.tug raz.at | ICQ #320057056 | |
    |-------------------University of Technology, Graz----------------|

    Comment

    • Rolf Magnus

      #17
      Re: #define vs. const

      Ioannis Vranos wrote:
      [color=blue]
      > Nicolas Pavlidis wrote:
      >[color=green]
      >> You mean the assert - macro defined in cassert, what Chris means is
      >> something like boost::STATIC_A SSERT, which makes the ckeck at
      >> compiletime.[/color]
      >
      >
      > How could this be possible for non compile-time constants?[/color]

      Not.

      Comment

      • Rolf Magnus

        #18
        Re: #define vs. const

        Eric Schmidt wrote:
        [color=blue]
        > Rolf Magnus <ramagnus@t-online.de> wrote in message
        > news:<cijv2g$g5 4$05$1@news.t-online.com>...[color=green]
        >> Even further, that may introduce undefined behaviour, like in the
        >> infamous example:
        >>
        >> #define MIN(x, y) ((x) < (y) ? (x) : (y))
        >>
        >> //...
        >> int a = 3;
        >> int b = 5;
        >> int c = MIN(a++, b--);[/color]
        >
        > There is no undefined behavior in this fragment.[/color]

        Hmm. I guess you're right. Let's just use the SQUARE example then:

        #define SQUARE(x) ((x)*(x))

        //...
        int x = 3;
        SQUARE(x++);

        Comment

        • Markus Elfring

          #19
          Re: #define vs. const

          > > #define MIN(x, y) ((x) < (y) ? (x) : (y))[color=blue][color=green]
          > > [...]
          > > int c = MIN(a++, b--);[/color]
          >
          > There is no undefined behavior in this fragment.[/color]

          The fragment might show "unexpected " behavior. Each increment and
          decrement expression can be evaluated up to twice.

          Comment

          • Thomas Matthews

            #20
            Re: #define vs. const

            Robert wrote:[color=blue]
            > Greetings everyone,
            >
            > I was wondering if a const variable or object took up space. I know
            > that a #define'd macro doesn't, as it's basically just interpreted by
            > the compiler. If a const does take up space, is there any reason to
            > choose it over a #define'd constant?
            >
            > -- Thank you
            >
            > P.S. if it makes any difference, I ssh to a SunOs machine where I use
            > g++ as my compiler.[/color]

            In the case where the const object is used in an expression,
            it may take up space:
            #include <iostream>
            #include <cstdlib>

            const unsigned int five = 5;

            int main(void)
            {
            std::cout << five << std::endl;
            return EXIT_SUCCESS;
            }

            However, it may not take up any space in this example:

            unsigned char array[five];

            The best method to find out what is being placed in your code is to
            generate an assembly listing using the compiler or on the executable
            using a debugger.

            --
            Thomas Matthews

            C++ newsgroup welcome message:

            C++ Faq: http://www.parashift.com/c++-faq-lite
            C Faq: http://www.eskimo.com/~scs/c-faq/top.html
            alt.comp.lang.l earn.c-c++ faq:

            Other sites:
            http://www.josuttis.com -- C++ STL Library book

            Comment

            Working...