# Std::unique\_ptr

**URL:** <https://forum.juce.com/t/std-unique-ptr/27860>\
**Category:** General JUCE discussion\
**Created:** [May 25, 2018, 6:54am UTC](https://forum.juce.com/t/std-unique-ptr/27860 "2018-05-25T06:54:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nammick](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nammick/32/2169_2.png) [@nammick](https://forum.juce.com/u/nammick)\
**Post date:** [May 25, 2018, 6:54am UTC](https://forum.juce.com/t/std-unique-ptr/27860/1 "2018-05-25T06:54:17Z")

</div>

So I noticed in the Main.cpp that ScopedPointer mainWindow; changed to std::unique\_ptr mainWindow; Coming from a C# I have been contented just using new and delete concept as this made sense to me (I have been bitten a few times with the sequencencing when destroying and access violations on threads). Reading the roadmap saying that juce library wants to replace out ScopedPointer in favour of the standard libraries unique\_ptr is this the correct way of using in a juce context or is there going to be a different way?

```
public:
    std::unique_ptr<Header> header;

```

then used…

addAndMakeVisible( (header = std::make\_unique()).get() );

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 25, 2018, 7:04am UTC](https://forum.juce.com/t/std-unique-ptr/27860/2 "2018-05-25T07:04:12Z")

</div>

If this object has a default constructor, then why use a pointer at all? Unless you actually _need_ it to be a pointer, just using a member variable is by far the best way of handling object lifetimes.

```auto
public:
    Header header;

addAndMakeVisible (header);

```

---

<div class="post-metadata">

**Author:** ![nammick](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nammick/32/2169_2.png) [@nammick](https://forum.juce.com/u/nammick)\
**Post date:** [May 25, 2018, 7:46am UTC](https://forum.juce.com/t/std-unique-ptr/27860/3 "2018-05-25T07:46:10Z")

</div>

Cheers, yeah I usually do that with controls such as knobs, sliders, buttons e.t.c. but I do find it handier to have things like scenes to be pointers as I have an extended component class with a few extra methods for passing info and events between. Just checking that I wasn’t doing anything stupid if I did it that way?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 25, 2018, 7:49am UTC](https://forum.juce.com/t/std-unique-ptr/27860/4 "2018-05-25T07:49:42Z")

</div>

Yeah, it’s fine.

I think it’d read better like this though:

```auto
header = std::make_unique<Header>();
addAndMakeVisible (*header);
```

---

<div class="post-metadata">

**Author:** ![bwall](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/bwall/32/618_2.png) [@bwall](https://forum.juce.com/u/bwall)\
**Post date:** [May 25, 2018, 2:56pm UTC](https://forum.juce.com/t/std-unique-ptr/27860/5 "2018-05-25T14:56:29Z")

</div>

Or you may prefer to do it all on one line

`addAndMakeVisible(*(header = std::make_unique<Header>()));`,

which is how I ended up doing it when I refactored to remove all the ScopedPointers.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 25, 2018, 4:04pm UTC](https://forum.juce.com/t/std-unique-ptr/27860/6 "2018-05-25T16:04:32Z")

</div>

It’s a common use-case. I wonder if it would make sense to add a templated method to Component like this?

```auto
template <typename ChildCompType, template... Parameters>
std::unique_ptr<ChildCompType> createChild (Parameters...);

```

…so you could write

```auto

struct ParentComp
{
    ParentComp()
    {
        child = createChild<MyChild> (1234);
    }

    std::unique_ptr<MyChild> child;

```

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [May 25, 2018, 4:24pm UTC](https://forum.juce.com/t/std-unique-ptr/27860/7 "2018-05-25T16:24:12Z")

</div>

I was about to propose adding that. 🙂

---

<div class="post-metadata">

**Author:** ![matkatmusic](https://avatars.discourse-cdn.com/v4/letter/m/dfb087/32.png) [@matkatmusic](https://forum.juce.com/u/matkatmusic)\
**Post date:** [October 1, 2018, 4:47am UTC](https://forum.juce.com/t/std-unique-ptr/27860/8 "2018-10-01T04:47:16Z")

</div>

I try to use this syntax:

```auto
header = std::make_unique<Header>();
addAndMakeVisible( header.get() );

```

`addAndMakeVisible` takes references **and** pointers. imo, the syntax for pointer dereferencing is super ugly, so I avoid it as much as possible, unless absolutely required.

---

<div class="post-metadata">

**Author:** ![lalala](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/lalala/32/8577_2.png) [@lalala](https://forum.juce.com/u/lalala)\
**Post date:** [October 1, 2018, 8:50am UTC](https://forum.juce.com/t/std-unique-ptr/27860/9 "2018-10-01T08:50:40Z")

</div>

Not sure if the juce team as a preference, as both notations

```
header = std::make_unique<Header>();
addAndMakeVisible (*header);

```

and

```
header = std::make_unique<Header>();
addAndMakeVisible (header.get());

```

are used in the codebase. perhaps it could be made consistent and part of the code style guidelines?  
My vote goes for \*

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [October 1, 2018, 9:13am UTC](https://forum.juce.com/t/std-unique-ptr/27860/10 "2018-10-01T09:13:20Z")

</div>

Personally I would prefer the `addAndMakeVisible (Component&)` overload because it’s _slightly_ more efficient but also clearer if you know the object is valid.

Personally I often do:  
`addAndMakeVisible (*(header = std::make_unique<Header>()))`  
but I’m aware that’s a little parenthesis heavy.

Jule’s suggestion or even a templated pointer version could work?

```auto
template<typename ComponentPointerType>
addAndMakeVisible (ComponentPointerType&& ptr)
{
    if (ptr != nullptr)
        addAndMakeVisible (*ptr);
}

```

could work…
