15

I have code that looks like this:

class T {};

class container {
 const T &first, T &second;
 container(const T&first, const T & second);
};

class adapter : T {};

container(adapter(), adapter());

I thought lifetime of constant reference would be lifetime of container. However, it appears otherwise, adapter object is destroyed after container is created, leaving dangling reference.

What is the correct lifetime?

is stack scope of adapter temporary object the scope of container object or of container constructor?

how to correctly implement binding temporary object to class member reference?

Thanks

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
Anycorn
  • 50,217
  • 42
  • 167
  • 261

5 Answers5

17

According to the C++03 standard, a temporary bound to a reference has differing lifetimes depending on the context. In your example, I think the highlighted portion below applies (12.2/5 "Temporary objects"):

The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference except as specified below. A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits. A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call.

So while binding a temporary is an advanced technique to extend the lifetime of the temporary object (GotW #88: A Candidate For the "Most Important const"), it apparently won't help you in this case.

On the other hand, Eric Niebler has an article that you may be interested in that discusses an interesting (if convoluted) technique that could let your class's constructors deduce whether a temporary object (actually an rvalue) has been passed to it (and therefore would have to be copied) or a non-temporary (lvalue) as been passed (and therefore could potentially safely have a reference stashed away instead of copying):

Good luck with it though - every time I read the article, I have to work through everything as if I've never seen the material before. It only sticks with me for a fleeting moment...

And I should mention that C++0x's rvalue references should make Niebler's techniques unnecessary. Rvalue references will be supported by MSVC 2010 which is scheduled to be released in a week or so (on 12 April 2010 if I recall correctly). I don't know what the status of rvalue references is in GCC.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • I think actually in this case the temporary is bound to a function call parameter (the constructor call) as in the next sentence. Yes, it's also bound to the member because of the aliasing in the ctor initializer, and yes, it will persist until the constructor exits (longer, in fact, if the full-expression containing the constructor call also does other things). But I think the highlighted passage refers to stuff like `struct container { const &adapter a; container() : a(adapter()) {} };`. – Steve Jessop Apr 09 '10 at 00:23
  • @Steve: on closer look, I think you're right - I'll update the answer (same result though). – Michael Burr Apr 09 '10 at 00:25
6

Temporary const references only have the lifetime of the current statement (that is, they go out of scope just before the semi-colon). So the rule of thumb is never rely on a const-reference existing beyond the lifetime of the function that receives it as a parameter, in this case that's just the constructor. So once the constructor is done, don't rely on any const references to still be around.

There is no way to change/override/extend this lifetime for temporaries. If you want a longer lifetime, use an actual object and not a temporary:

adapter a, b; 
container(a, b); // lifetime is the lifetime of a and b

Or better yet, just don't use constant references to class members except in the most dire circumstances when the objects are very closely related and definitely not temporary.

SoapBox
  • 20,457
  • 3
  • 51
  • 87
  • 2
    To be more precise, they live until the end of the full expression they were created in. – GManNickG Apr 09 '10 at 00:00
  • 1
    "There is no way to change/override/extend this lifetime for temporaries" - actually there is, it's just not useful in cases like this. If you use a temporary to initialize a const reference with automatic duration, then the lifetime of the temporary is extended until the scope of the automatic is exited. – Steve Jessop Apr 09 '10 at 00:08
1

The reference will exist for the entire lifetime of container, but the object being referenced will exist only for the lifetime of that object. In this case, you have bound your reference to a temporary object with automatic storage allocation ("stack allocation", if you will, although that isn't C++ nomenclature). Therefore, you cannot expect the temporary to exist beyond the statement in which it was written (as it goes out of scope immediately after the call to the constructor for container). The best way to deal with this is to use a copy, instead of a reference. Since you are using a const reference, anyway, it will have similar semantics.

You should redefine your class as:

template<typename T> 
class container 
{
    public:
        container(const T& first, const T& second) : first(first), second(second) {}
    private:
        const T first;
        const T second;
};

Alternatively, you could give your objects a name to prevent them from going out of scope:

   adaptor first;
   adaptor second;
   container c(first,second);

However, I don't think this a good idea, since a statement such as return c is invalid.

Edit
If your goal is to share objects in order to avoid the cost of copying, then you should consider using smart pointer objects. For example, we can redefine your object using smart pointers as follows:

template<typename T> 
class container 
{
    public:
        container(const boost::shared_ptr<const T>& first, const boost::shared_ptr<const T>& second) : first(first), second(second) {}
    private:
        boost::shared_ptr<const T> first;
        boost::shared_ptr<const T> second;
};

You can then use:

boost::shared_ptr<const adaptor> first(new adaptor);
boost::shared_ptr<const adaptor> second(new adaptor);
container<adaptor> c(first,second);

Or, if you want to have mutable copies of first and second locally:

boost::shared_ptr<adaptor> first(new adaptor);
boost::shared_ptr<adaptor> second(new adaptor);
container<adaptor> c(boost::const_pointer_cast<const adaptor>(first),boost::const_pointer_cast<const adaptor>(second));
Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
0

If you want to avoid copying, then I suppose the Container must create the stored instances itself.

If you want to invoke the default constructor, then it should be no problem. Just invoke the default constructor of Container.

It is probably more problematic if you want to invoke a non-default constructor of the contained type. C++0x will have better solutions for that.

As an excercise, the container can accept a T, or an object containing the arguments for the constructor of T. This still relies on RVO (return value optimization).

template <class T1>
class construct_with_1
{
    T1 _1;
public:
    construct_with_1(const T1& t1): _1(t1) {}
    template <class U>
    U construct() const { return U(_1); }
};

template <class T1, class T2>
class construct_with_2
{
    T1 _1;
    T2 _2;
public:
    construct_with_2(const T1& t1, const T2& t2): _1(t1), _2(t2) {}
    template <class U>
    U construct() const { return U(_1, _2); }
};

//etc for other arities

template <class T1>
construct_with_1<T1> construct_with(const T1& t1)
{
    return construct_with_1<T1>(t1);
}

template <class T1, class T2>
construct_with_2<T1, T2> construct_with(const T1& t1, const T2& t2)
{
    return construct_with_2<T1, T2>(t1, t2);
}

//etc
template <class T>
T construct(const T& source) { return source; }

template <class T, class T1>
T construct(const construct_with_1<T1>& args)
{
    return args.template construct<T>();
}

template <class T, class T1, class T2>
T construct(const construct_with_2<T1, T2>& args)
{
    return args.template construct<T>();
}

template <class T>
class Container
{
public:
    T first, second;

    template <class T1, class T2>
    Container(const T1& a = T1(), const T2& b = T2()) : 
        first(construct<T>(a)), second(construct<T>(b)) {}
}; 

#include <iostream>

class Test
{
    int n;
    double d;
public:
    Test(int a, double b = 0.0): n(a), d(b) { std::cout << "Test(" << a << ", " << b << ")\n"; }
    Test(const Test& x): n(x.n), d(x.d) { std::cout << "Test(const Test&)\n"; }
    void foo() const { std::cout << "Test.foo(" << n << ", " << d << ")\n"; }
};

int main()
{
    Test test(4, 3.14);
    Container<Test> a(construct_with(1), test); //first constructed internally, second copied
    a.first.foo();
    a.second.foo();
}
visitor
  • 8,564
  • 2
  • 26
  • 15
-1

Don't do this. A temporary is destroyed immediately after the expression in which it was created (except in the case that it's immediately bound to a reference, in which case it's the scope of the reference). The lifetime cannot be extended to that of the class.

This is why I never store members as references - only copied objects or pointers. To me, pointers make it obvious that the lifetime comes in to play. Especially in the case of a constructor, it's non-obvious that your constructor params must outlive the class itself.

Stephen
  • 47,994
  • 7
  • 61
  • 70
  • 1
    -1: Pointers should be replaced with references whenever possible. – Billy ONeal Apr 09 '10 at 00:01
  • I didn't -1, but they live until the end of the full expression they were created in, not the scope. – GManNickG Apr 09 '10 at 00:03
  • First of all, that's a ridiculous statement. Second of all, in this case references make this behavior entirely non-obvious. Lame -1. – Stephen Apr 09 '10 at 00:03
  • GMan - the difference being in a case like "const string& ref = create_temporary_string();". In that case, it's bound to the scope of the reference. – Stephen Apr 09 '10 at 00:05
  • +1 : I disagree with pointer hate. In this case, pointers make it explicit that what's being referenced lives somewhere else in the ether and we have no idea whether it is valid or not. They also have the added feature of NULL/o/nullptr, which hopefully would allow us to ask the pointer if it was still valid (there's no way to ask that of a reference). – SoapBox Apr 09 '10 at 00:05
  • @Stephen: Yes. So entirely non obvious that it's the default behavior for every other major object oriented programming language (except objective C). Pointers are a C relic that can give you expressive power. But if you're not using that power, then the pointer needs to be replaced with the reference. – Billy ONeal Apr 09 '10 at 00:05
  • @Stephen: I'm replying to the text: "The lifetime of the temporary is the scope of the temporary." If you want to clarify that, please do, but it's not correct. (In the simplest case, which is what the text states.) – GManNickG Apr 09 '10 at 00:07
  • @Billy, that "the default behavior" is usually accompanied with garbage collection, so holding references to outdated objects is a non-issue. – Stephen Apr 09 '10 at 00:09
  • @Stephen: Considering the object in question should never be `delete` ing the pointer I fail to see how garbage collection has anything at all to do with references versus pointers. – Billy ONeal Apr 09 '10 at 00:13
  • @Billy, because in the case of Object(Param(), OtherParam()), the references to the parameter objects don't last long enough in C++. It's non-obvious to the reader of the code that this is a bug. In Java/Python/etc the params are ref-counted and so they live long enough. We can agree to disagree on our opinion of ptrs/references, and agree to stay out of each-others code bases :) – Stephen Apr 09 '10 at 00:16
  • 1
    @Billy ONeal: not really, a lot of those other major OO languages have re-seatable, nullable references. Since C++'s references are not nullable or re-seatable, it doesn't make a whole lot of sense to say, "well, Java uses references therefore C++ code should use references". The references aren't the same. Anyway, using a pointer doesn't actually force you to do pointer arithmetic, and it's avoiding that which leads those other languages to avoid pointers. I note with interest that Go has pointers, but no pointer arithmetic, and no separate pointer-member-access operator. – Steve Jessop Apr 09 '10 at 00:33
  • @Steve Jessop: I'm not saying that someone should use references because that's how other languages do it. I'm just saying that references are not "entirely non-obvious". – Billy ONeal Apr 09 '10 at 01:29
  • @Billy: Nobody said that references are "entirely non-obvious". What was said was that taking a constructor param by reference and storing it as a member variable makes it non-obvious at the call-site that the reference needs to outlive the class. – Stephen Apr 09 '10 at 01:49