rvalue reference as an object field

Viewed 1084
#include <iostream>  
struct Bar
{
    int nb_;
    Bar(int nb) :nb_(nb){}
    ~Bar()
    {
        std::cout << "~Bar" << "\n";
    }
}; 

struct Foo
{    
    template<class T>
    Foo(T&& param) :bar_(std::move(param))
    {
        std::cout << "Foo" << "\n";
    } 
    ~Foo()
    {
        std::cout << "~Foo" << "\n";
    }

    Bar&& bar_;
};

int main()
{       
    { 
        Foo foo(Bar(1));
    }

    std::cin.ignore();
}

//Output:
Foo
~Bar 
~Foo

Is this program is legal C++? Does bar_ will become a dangling reference once the Foo constructor will be finished?

If this is not legal in term of C++ standard, when is it useful to have a rvalue reference as a field ?

2 Answers
Related