What does the passage mean on const Parameters?

Discussion in 'C++' started by fl, Jun 15, 2010.

  1. fl

    fl Guest

    Hi,
    I am learning C++ with "C++ primer" Fourth edition by Stanley B.
    Lippman, Josee Lajoie and Barbara E. Moo.
    I have a difficuty when I learn Chapter 7. Funtions. More
    specifically, 7.2.1. Nonreference Parameters.
    In passage on "const Parameters", it first gives an example:

    const int i = 3, j = 6;
    int k = gcd(3, 6); // ok: k initialized to 3

    It says that it works no matter int or const int for gcd's argument.



    My problem is the following. I copy the detail contents here. I type
    these words as possible as the original text except I cannot type
    italic font here. I don't understand the meaning of these paragraphs.
    What is the implication of the two void fcn function? Does these
    paragraphs tell me that

    void fcn(const int i) { /* fcn can read butnot write to i */ }

    is not supported by C++?

    Could you explain it to me? Thanks.


    The most puzzled me is the except from below:
    ---------------------------------
    What may be surprising, is that although the parameter is a const
    inside the function, the compiler otherwise treats the definition of
    fcn as if we had defined the parameter as a plain int:

    void fcn(const int i) { /* fcn can read but not write to i */ }
    void fcn(int i) { /* .........*/ } // error:
    redefines fcn(int)
    ---------------------------------




    .............
    If we make the parameter a const nonreference type:

    void fcn(const int i) { /* fcn can read butnot write to i */ }

    then the function cannot change its local copy of the argument. The
    argument is still passed as a copy so we can pass fcn either a const
    or nonconst object.

    What may be surprising, is that although the parameter is a const
    inside the function, the compiler otherwise treats the definition of
    fcn as if we had defined the parameter as a plain int:

    void fcn(const int i) { /* fcn can read but not write to i */ }
    void fcn(int i) { /* .........*/ } // error:
    redefines fcn(int)

    This usage exists to support compatibility with the C language, which
    makes no distinction between functions taking const or nonconst
    parameters.

    .......................................................
    fl, Jun 15, 2010
    #1
    1. Advertising

  2. * fl, on 15.06.2010 05:59:
    > Hi,
    > I am learning C++ with "C++ primer" Fourth edition by Stanley B.
    > Lippman, Josee Lajoie and Barbara E. Moo.
    > I have a difficuty when I learn Chapter 7. Funtions. More
    > specifically, 7.2.1. Nonreference Parameters.
    > In passage on "const Parameters" [...}:


    OK. This is about whether two declarations of a function refer to the same
    function or not. And they do refer to the same function if the names are the
    same and the *function type* is the same in both declarations.

    The function type is deduced from your declaration by sort of rewriting the
    formal argument types, including that

    * top level 'const' for a formal argument is removed, e.g.
    'int const a' -> 'int a',

    * function types are converted to function pointer types, e.g.
    'void callback( int x )' -> 'void (*callback)( int x )', and

    * arrays (but not references to arrays) are converted to pointers,
    completely losing and disregarding any stated array size, e.g.
    'int weights[42]' -> 'int* weights'.

    So when you write

    void foo( int x ) {}
    void foo( int const x ) {}

    the compiler sees the function types, which together with the names *identify*
    them, as

    void foo( int );
    void foo( int );

    and complains that you have two or more definitions of the same function, which
    effectively means that you cannot overload solely on top-level 'const'.

    The 'const' is honored within the function definition, but it's not part of the
    identifying type.

    Almost the same would happen with e.g.

    void foo( int weights[42] );
    void foo( int* age );

    where all that the compilers sees wrt. identification is

    void foo( int* );
    void foo( int* );

    However, unlike the 'const' the formal argument's array size is not effective
    within the function definition. With 'const' the only effect of the rewriting is
    for identification of the function. With array and function-type arguments the
    effect is as if the rewriting had been performed on the source code: the result
    after the rewriting is all that you have inside the function definition.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach, Jun 15, 2010
    #2
    1. Advertising

  3. * Alf P. Steinbach, on 15.06.2010 06:19:
    > * fl, on 15.06.2010 05:59:
    >> Hi,
    >> I am learning C++ with "C++ primer" Fourth edition by Stanley B.
    >> Lippman, Josee Lajoie and Barbara E. Moo.
    >> I have a difficuty when I learn Chapter 7. Funtions. More
    >> specifically, 7.2.1. Nonreference Parameters.
    >> In passage on "const Parameters" [...}:

    >
    > OK. This is about whether two declarations of a function refer to the
    > same function or not. And they do refer to the same function if the
    > names are the same and the *function type* is the same in both
    > declarations.
    >
    > The function type is deduced from your declaration by sort of rewriting
    > the formal argument types, including that
    >
    > * top level 'const' for a formal argument is removed, e.g.
    > 'int const a' -> 'int a',
    >
    > * function types are converted to function pointer types, e.g.
    > 'void callback( int x )' -> 'void (*callback)( int x )', and
    >
    > * arrays (but not references to arrays) are converted to pointers,
    > completely losing and disregarding any stated array size, e.g.
    > 'int weights[42]' -> 'int* weights'.
    >
    > So when you write
    >
    > void foo( int x ) {}
    > void foo( int const x ) {}
    >
    > the compiler sees the function types, which together with the names
    > *identify* them, as
    >
    > void foo( int );
    > void foo( int );
    >
    > and complains that you have two or more definitions of the same
    > function, which effectively means that you cannot overload solely on
    > top-level 'const'.
    >
    > The 'const' is honored within the function definition, but it's not part
    > of the identifying type.
    >
    > Almost the same would happen with e.g.
    >
    > void foo( int weights[42] );
    > void foo( int* age );
    >
    > where all that the compilers sees wrt. identification is
    >
    > void foo( int* );
    > void foo( int* );
    >
    > However, unlike the 'const' the formal argument's array size is not
    > effective within the function definition. With 'const' the only effect
    > of the rewriting is for identification of the function. With array and
    > function-type arguments the effect is as if the rewriting had been
    > performed on the source code: the result after the rewriting is all that
    > you have inside the function definition.


    Oh, I should add, before someone else does, that return types are disregarded
    with respect to function identity. Sorry for forgetting to mention that. It
    means that you cannot overload solely on return type, either.


    Cheers,

    - Alf


    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach, Jun 15, 2010
    #3
    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. sysindex
    Replies:
    1
    Views:
    322
    Victor Bazarov
    Jun 22, 2004
  2. Javier
    Replies:
    2
    Views:
    548
    James Kanze
    Sep 4, 2007
  3. Chris
    Replies:
    3
    Views:
    157
    Mick White
    Aug 5, 2004
  4. Replies:
    0
    Views:
    118
  5. Replies:
    0
    Views:
    103
Loading...

Share This Page