A const behavior I don't understand

Discussion in 'C++' started by Ben Thomas, Jun 19, 2008.

  1. Ben Thomas

    Ben Thomas Guest

    Hello,

    I have the following code which I don't understand why it works :

    #include <iostream>
    using namespace std;

    void DontWork (unsigned int& i) {
    cout << i << endl;
    }

    void Work (unsigned int const& i) {
    cout << i << endl;
    }

    void main () {
    signed int i = 1;
    // DontWork (i);
    Work (i);
    }

    What I don't understand is why the compiler can find an implicit
    conversion when I add the const modifier, but it is unable to do so
    without it.

    (I know I should not code like this, but I just want to understand the
    behavior.)

    Thank you,
    Ben.
    Ben Thomas, Jun 19, 2008
    #1
    1. Advertising

  2. Ben Thomas

    Ben Thomas Guest

    Thanks Mike for the clarification, I understand why it works/don't works
    now.

    Ben.

    Mike Wahler wrote:
    > "Ben Thomas" <> wrote in message
    > news:8hv6k.12$...
    >> Hello,
    >>
    >> I have the following code which I don't understand why it works :
    >>
    >> #include <iostream>
    >> using namespace std;
    >>
    >> void DontWork (unsigned int& i) {
    >> cout << i << endl;
    >> }
    >>
    >> void Work (unsigned int const& i) {
    >> cout << i << endl;
    >> }
    >>
    >> void main () {
    >> signed int i = 1;
    >> // DontWork (i);
    >> Work (i);
    >> }
    >>
    >> What I don't understand is why the compiler can find an implicit
    >> conversion when I add the const modifier, but it is unable to do so
    >> without it.
    >>
    >> (I know I should not code like this, but I just want to understand the
    >> behavior.)
    >>
    >> Thank you,
    >> Ben.

    >
    > I compiled your code and looked up the error code (MSVC++). See
    > the last two paragraphs .
    >
    > ====================================================================
    > Error Message
    > 'function' : cannot convert parameter number from 'type1' to 'type2'
    >
    > A parameter cannot be converted to the required type. This might
    > happen if you create an instance of a class and attempt an implicit
    > conversion on a constructor marked with the explicit keyword.
    >
    > If a temporary object is passed to a function that takes a reference
    > to an object as a parameter, that reference must be a const reference.
    >
    > If the function is called with a parameter that is not of the type
    > that the function expects, a temporary object is created using the
    > appropriate constructor. This temporary object is then passed to
    > the function. In this case, the temporary object is used to initialize
    > the reference. In earlier versions of the language, all references
    > could be initialized by temporary objects.
    > ====================================================================
    >
    >
    > Of course a particular implementation is not authoritative about the
    > language standard, but the above explanation makes sense to me.
    >
    >
    > BTW main()'s required (by the standard) return type is 'int', even
    > if a compiler lets you get away with 'void'.
    >
    > -Mike
    >
    >
    Ben Thomas, Jun 19, 2008
    #2
    1. Advertising

  3. Ben Thomas

    Triple-DES Guest

    On 19 Jun, 17:54, Ben Thomas <> wrote:
    > Hello,
    >
    > I have the following code which I don't understand why it works :
    >
    > #include <iostream>
    > using namespace std;
    >
    > void DontWork (unsigned int& i) {
    >         cout << i << endl;
    >
    > }
    >
    > void Work (unsigned int const& i) {
    >         cout << i << endl;
    >
    > }
    >
    > void main () {
    >         signed int i = 1;
    >         // DontWork (i);
    >         Work (i);
    >
    > }
    >
    > What I don't understand is why the compiler can find an implicit
    > conversion when I add the const modifier, but it is unable to do so
    > without it.


    In the process of converting from 'int' to 'unsigned int' a temporary
    object is created. You cannot bind a non-const reference to a
    temporary object.

    Consider the example:
    #include <iostream>
    int main ()
    {
    int i = 1;
    // fails: binding a non-const ref to a temporary
    unsigned& uref = i;

    // ok, binding const ref to temporary
    const unsigned& curef = i;

    i = 2;
    if( i != curef)
    std::cout << "curef is not bound to i";
    }

    You may think that the reference curef is bound to i, but it is not,
    it is bound to an unnamed temporary object, as illustrated by the
    example.

    DP
    Triple-DES, Jun 20, 2008
    #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. johny smith
    Replies:
    7
    Views:
    304
  2. camotito

    QuickSort . I don't understand this behavior

    camotito, Apr 24, 2006, in forum: C Programming
    Replies:
    2
    Views:
    273
    Michael Mair
    Apr 24, 2006
  3. Javier
    Replies:
    2
    Views:
    541
    James Kanze
    Sep 4, 2007
  4. Heck
    Replies:
    9
    Views:
    386
  5. Martin P. Hellwig
    Replies:
    1
    Views:
    362
    Martin P. Hellwig
    Mar 26, 2010
Loading...

Share This Page