Question regarding Const Reference and Temporaries

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

    Question regarding Const Reference and Temporaries

    Hi all,

    Consider the following code fragment:

    // some data structure
    class Data { ... }

    // Container for the data structure
    Class Container
    {
    public:
    Container(const & Data data): m_cRefData(data ), m_ncRefData(dat a)
    {}


    private:
    const Data& m_cRefData;
    Data& m_ncRefData;
    };

    // some func to create the data
    Data getData(int i, int j, string k, const char* l)
    {
    return Data(i,j,k,l);
    }

    // some func to process the container
    void processContaine r(Container& container)
    {
    // work with container.mcRef Data and/or m_ncRefData
    }


    // some code somewhere ...
    Container container( getData(i,j,k,l ) );

    // .... do some other things ... amount of time taken may varies ...

    processContaine r(container);


    My question is what happen if I used the data member of the container?
    Will the value initialized in the Container constructor still valid?
    I read something about temporaries bind to const& have a lifetime of
    the const&. If that holds, container.mncRe fData may not be valid by
    the time it is process but, container.mcRef Data is still valid by the
    time it is processed. Is that assumption correct?

    Thanks,
    Kaede
  • Kevin Goodsell

    #2
    Re: Question regarding Const Reference and Temporaries

    kaede wrote:
    [color=blue]
    > Hi all,
    >
    > Consider the following code fragment:
    >
    > // some data structure
    > class Data { ... }
    >
    > // Container for the data structure
    > Class Container[/color]

    class, not Class.
    [color=blue]
    > {
    > public:
    > Container(const & Data data): m_cRefData(data ), m_ncRefData(dat a)
    > {}[/color]

    & is in the wrong place.

    Container(const Data &data)

    Also, I don't think you can initialize a non-const reference with a
    const reference. I've never tried it or bothered to look it up in the
    standard, but I'd be very surprised if it were legal.
    [color=blue]
    >
    >
    > private:
    > const Data& m_cRefData;
    > Data& m_ncRefData;
    > };
    >
    > // some func to create the data
    > Data getData(int i, int j, string k, const char* l)
    > {
    > return Data(i,j,k,l);
    > }
    >
    > // some func to process the container
    > void processContaine r(Container& container)
    > {
    > // work with container.mcRef Data and/or m_ncRefData
    > }
    >
    >
    > // some code somewhere ...
    > Container container( getData(i,j,k,l ) );[/color]

    The temporary returned from getData is destroyed after this expression
    completes. Your references are no longer valid.
    [color=blue]
    >
    > // .... do some other things ... amount of time taken may varies ...
    >
    > processContaine r(container);
    >
    >
    > My question is what happen if I used the data member of the container?[/color]

    Undefined behavior.
    [color=blue]
    > Will the value initialized in the Container constructor still valid?
    > I read something about temporaries bind to const& have a lifetime of
    > the const&.[/color]

    Not quite what happened here. The reference you bound the temporary to
    was the parameter to the constructor. It died when the constructor finished.

    In general, the compiler would have no way of knowing that you made
    *another* reference to the temporary (if the construct were in a
    different translation unit, for example), so it could not possibly
    prolong the life of the temporary to match the life of the reference.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.

    Comment

    • WW

      #3
      Re: Question regarding Const Reference and Temporaries

      Kevin Goodsell wrote:[color=blue][color=green]
      >> Container(const & Data data): m_cRefData(data ),
      >> m_ncRefData(dat a) {}[/color]
      >
      > & is in the wrong place.
      >
      > Container(const Data &data)[/color]

      Or if we are at it:

      Container(Data const &data)

      ;-)
      [color=blue]
      > Also, I don't think you can initialize a non-const reference with a
      > const reference. I've never tried it or bothered to look it up in the
      > standard, but I'd be very surprised if it were legal.[/color]

      Only in The Sage mode. ;-) Can be activated by
      the --allow-all-sort-of-silly-errors-and-swearing mode.

      --
      WW aka Attila


      Comment

      • kaede

        #4
        Re: Question regarding Const Reference and Temporaries

        Kevin Goodsell <usenet1.spamfr ee.fusion@never box.com> wrote in message news:<dXsfb.147 5$gA1.365@newsr ead3.news.pas.e arthlink.net>.. .[color=blue]
        > Not quite what happened here. The reference you bound the temporary to
        > was the parameter to the constructor. It died when the constructor finished.
        >
        > In general, the compiler would have no way of knowing that you made
        > *another* reference to the temporary (if the construct were in a
        > different translation unit, for example), so it could not possibly
        > prolong the life of the temporary to match the life of the reference.
        >
        > -Kevin[/color]

        Thanks, I think I understand the concept now. At first I thought, the
        second reference that bound to the temporary would prolong the life of
        the temporaries. I guess I made a wrong assumption there. I wrote
        some code to try out the scenerio and it work just fine. I guess that
        was just sheer luck.

        Again Thanks for the advice.
        Kaede

        Comment

        Working...