Question regarding Const Reference and Temporaries

K

kaede

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
 
K

Kevin Goodsell

kaede said:
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
 
W

WW

Kevin said:
& 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.
 
K

kaede

Kevin Goodsell said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top