By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,905 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,905 IT Pros & Developers. It's quick & easy.

Question regarding Const Reference and Temporaries

P: n/a
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(data)
{}
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 processContainer(Container& container)
{
// work with container.mcRefData and/or m_ncRefData
}
// some code somewhere ...
Container container( getData(i,j,k,l) );

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

processContainer(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.mncRefData may not be valid by
the time it is process but, container.mcRefData is still valid by the
time it is processed. Is that assumption correct?

Thanks,
Kaede
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
kaede wrote:
Hi all,

Consider the following code fragment:

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

// Container for the data structure
Class Container
class, not Class.
{
public:
Container(const& Data data): m_cRefData(data), m_ncRefData(data)
{}
& 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.


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 processContainer(Container& container)
{
// work with container.mcRefData and/or m_ncRefData
}
// some code somewhere ...
Container container( getData(i,j,k,l) );
The temporary returned from getData is destroyed after this expression
completes. Your references are no longer valid.

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

processContainer(container);
My question is what happen if I used the data member of the container?
Undefined behavior.
Will the value initialized in the Container constructor still valid?
I read something about temporaries bind to const& have a lifetime of
the const&.


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.

Jul 19 '05 #2

P: n/a
WW
Kevin Goodsell wrote:
Container(const& Data data): m_cRefData(data),
m_ncRefData(data) {}
& is in the wrong place.

Container(const Data &data)


Or if we are at it:

Container(Data const &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.


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

--
WW aka Attila
Jul 19 '05 #3

P: n/a
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<dX****************@newsread3.news.pas.earthl ink.net>...
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


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
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.