const and standard types

Discussion in 'C++' started by cppsks, Aug 25, 2004.

  1. cppsks

    cppsks Guest

    What would be the rational for using const with standard types?

    const int & OR const unsigned long &

    Thanks.
    cppsks, Aug 25, 2004
    #1
    1. Advertising

  2. cppsks wrote:
    > What would be the rational for using const with standard types?
    >
    > const int & OR const unsigned long &


    Often a const reference to one of built-in types happens to be used
    in template code, like

    template <class T> void foo(T const& blah);
    ...
    foo(42);

    Nothing bad about it, AFAIK. I once did some testing and figured
    that passing 'double' by value was just slightly slower than passing
    it by a ref or by a pointer, but only in a debug version (or something
    like that).

    My rule is, "when in doubt, test". My other rule is, "for built-in
    types, pass by value".

    Victor
    Victor Bazarov, Aug 25, 2004
    #2
    1. Advertising

  3. cppsks wrote:
    > What would be the rational for using const with standard types?
    >
    > const int & OR const unsigned long &
    > ...


    Hmm... What kind of answer do you expect besides the obvious "to declare
    objects of standard types as constants"? The implications of declaring
    an object of standard type as constant are numerous.

    Constant objects of integral type can be used in integral constant
    expressions, while non-constant objects can't:

    const int a = 5;
    int b = 5;

    char aa[a]; // OK
    char bb; // ERROR

    Const-qualified references to standard types can be bound to rvalue objects:

    const int& cr = 5; // OK
    int& r = 5; // ERROR

    And so on...

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Aug 26, 2004
    #3
  4. cppsks

    Rolf Magnus Guest

    cppsks wrote:

    > What would be the rational for using const with standard types?


    Just the same as for using it with your own types.

    > const int & OR const unsigned long &


    Did you make references here for a reason?
    Rolf Magnus, Aug 26, 2004
    #4
  5. cppsks

    Siemel Naran Guest

    "cppsks" <> wrote in message
    news:cgipa9$203$...

    > What would be the rational for using const with standard types?
    >
    > const int & OR const unsigned long &


    It can happen as part of implicit tempalte instantiation.

    template <class T>
    void print(const T&);

    For an explicit function, I'd probably pass by value as in the release build
    it would probably be faster because the compiler could optimize without
    having to worry about aliasing. See Victor's post too.

    If you have a function and it receives a variable by value, but in the
    function definition you declare the value const, that's another story. Is
    this what you're asking about?

    void f(int);
    void f(const int i) {
    ...
    }
    Siemel Naran, Aug 26, 2004
    #5
  6. cppsks

    cppsks Guest

    "Siemel Naran" <> wrote in message
    news:imcXc.516998$...
    > "cppsks" <> wrote in message
    > news:cgipa9$203$...


    > If you have a function and it receives a variable by value, but in the
    > function definition you declare the value const, that's another story. Is
    > this what you're asking about?
    >
    > void f(int);
    > void f(const int i) {
    > ...
    > }
    >
    >


    Thanks for the response to all. Just a quick question about the above
    declartion and definition.
    What does the above convention mean?

    Thanks,

    skscpp
    cppsks, Aug 28, 2004
    #6
  7. cppsks

    Siemel Naran Guest

    "cppsks" <> wrote in message
    news:cgp0ue$d3c$...
    > "Siemel Naran" <> wrote in message


    > > void f(int);
    > > void f(const int i) {
    > > ...
    > > }
    > >
    > >

    >
    > Thanks for the response to all. Just a quick question about the above
    > declartion and definition.
    > What does the above convention mean?


    Usually we declare the function variables without top-level const, but may
    define them with const to prevent us from accidentally changing the
    variable, and making it clear to the person reading the code that in the
    body of function f, variable 'i' will not change its value. Doubtless, this
    practice is useless for short functions which are simple to understand
    anyway, but does pay off in larger and/or complicated functions.
    Siemel Naran, Aug 28, 2004
    #7
    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. Abhijit Bhadra
    Replies:
    2
    Views:
    7,431
    Ron Natalie
    Dec 1, 2004
  2. Replies:
    11
    Views:
    1,089
  3. Javier
    Replies:
    2
    Views:
    545
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,093
    Tim Rentsch
    Nov 10, 2008
  5. fungus
    Replies:
    13
    Views:
    873
    fungus
    Oct 31, 2008
Loading...

Share This Page