Question: Say I have the following structures

```
struct X
{
    X () = delete;
    X(const std::string &);
    //all other copy/move/assignment ctors PLUS dtor are defined, just the default is deleted
    /* members */
};

struct Y
{
    X & x_object;
    Y( const std::string & str): Y { *(new X { str }) } { }
    Y( X &obj): x_object(obj) { }
    ~Y() {
        // x_object is needed to be freed here, would
    }
};

```

How do I know if ***x\_object*** is allocated on the heap or the stack? If I declared ***x\_object*** as a `std::shared_ptr<X>` then it would mean I'd need to make a copy of **obj** in the Y ctor that takes an X object. Calling **delete** on objects allocated on the stack AFAIK is an undefined behaviours(§ 5.3.5. I'd have done a `delete &x_object` ). OR is there any other way I can perform this operation which MUST directly reflect in whatever X object that was passed(pretending for a moment that X objects are noncopyable )?

Answer: 
A way to solve your deallocation issue:

```
struct Y
{
public:
    Y(const std::string & str) :
        x_owned(new X { str }), // Have ownership of X
        x_object(*x_owned)
    {}
    Y(X &obj) : x_owned(), x_object(obj) {}
    ~Y() = default;
private:
    std::unique_ptr<X> x_owned;
    X& x_object;
};

```
You can't decide whether an object has automatic lifetime ("stack") or dynamic lifetime ("heap"), at least not in a portable way. It's also very non-idiomatic to do this even if you could.

In C++ you must decide and know who is responsible for allocating and deallocating an object. If you really need the flexibility, you could add parameter (or a different overload) to allow the user to indicate whether they want the object destroyed in the destructor or not.