I have a function which accepts a Large by const reference:
void func(const Large& param);
and a class which holds a Large:
class HoldsLarge {
public:
Large GetByValue() const { return l; };
private:
Large l;
}
If I do
HoldsLarge x;
func(x.GetByValue());
am I correct in understanding that a temporary will be copy constructed for x.GetByValue() which will by passed by reference to func? Is there something in the standard which will allow a compiler to omit the construction of the temporary altogether? After all, func only need a const reference to HoldsLarge::l.
I understand I could simply return HoldsLarge::l by const reference but I would like to prevent clients from accidentally creating a dangling reference.