# Replacing ScopedPointer with std::unique\_ptr

**URL:** <https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099>\
**Category:** General JUCE discussion\
**Created:** [January 24, 2018, 2:04pm UTC](https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099 "2018-01-24T14:04:26Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mashu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mashu/32/579_2.png) [@mashu](https://forum.juce.com/u/mashu)\
**Post date:** [January 24, 2018, 2:04pm UTC](https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099/1 "2018-01-24T14:04:26Z")

</div>

I’m trying to make more stuff more generically C++ and use the standard library more. So, seems like a good move to replace the Juce ScopedPointer with the std library unique\_ptr.

But i seem to often run into errors like this one:

`Invalid operands to binary expression ('juce::TextButton *' and 'std::unique_ptr<TextButton>')`

any advice?

---

<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:** [January 24, 2018, 2:07pm UTC](https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099/2 "2018-01-24T14:07:51Z")

</div>

Maybe because std::unique\_ptr does not have a conversion operator to the raw pointer? You need to get the raw pointer with the .get() method.

---

<div class="post-metadata">

**Author:** ![mashu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mashu/32/579_2.png) [@mashu](https://forum.juce.com/u/mashu)\
**Post date:** [January 24, 2018, 2:19pm UTC](https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099/3 "2018-01-24T14:19:51Z")

</div>

thank you. it was that simple 🙂

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [January 25, 2018, 10:05am UTC](https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099/4 "2018-01-25T10:05:30Z")

</div>

We would advise everyone to move from `ScopedPointer`s to `std::unique_ptr`s - we will be doing it ourselves shortly after the release of JUCE 5.3.

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [January 25, 2018, 11:03am UTC](https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099/5 "2018-01-25T11:03:53Z")

</div>

> [@t0m](#):
>
> We would advise everyone to move from ScopedPointers to std::unique\_ptrs - we will be doing it ourselves shortly after the release of JUCE 5.3.

I just tested changing 2 ScopedPointer(s) to std::unique\_ptr in my plug-in and I’m finding in my debug build I’m getting glitching while playing back grooves… I found this:

> <https://stackoverflow.com/questions/8138284/about-unique-ptr-performances>

Have y’all tested performance between ScopedPointer and std::unique\_ptr without optimizations?

Cheers,

Rail

---

<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:** [January 25, 2018, 12:14pm UTC](https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099/6 "2018-01-25T12:14:20Z")

</div>

That sounds very odd indeed! Certainly an important part of the design of std::unique\_ptr was that it’d optimise away to a raw pointer in almost all situations…

Are you sure the glitches aren’t something behaving differently because of a semantic difference?

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [January 25, 2018, 12:24pm UTC](https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099/7 "2018-01-25T12:24:40Z")

</div>

My report was more anecdotal – I just changed the ScopedPointers and tested playback… but I was also compiling using Parallels in a VM machine at the same time… undoing and re-doing the change appeared to indicate that the glitching only occurred with the std::unique\_ptr – but it may just have been coincidental… I’ll do proper testing later. I did the Google and found the linked article above, so was curious if y’all had done any testing with a Debug build.

Cheers,

Rail

---

<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:** [January 25, 2018, 2:25pm UTC](https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099/8 "2018-01-25T14:25:29Z")

</div>

No, not sure how you’d even go about benchmarking something as fundamental as that, TBH…

---

<div class="post-metadata">

**Author:** ![wesleylaurence](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@wesleylaurence](https://forum.juce.com/u/wesleylaurence)\
**Post date:** [September 6, 2020, 10:44pm UTC](https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099/9 "2020-09-06T22:44:30Z")

</div>

I am trying to replace ScopedPointer with std::unique\_ptr and am getting an error that says “no operator ‘=’ matches these operands”. Here is the code:

`std::unique_ptr<juce::Slider> driveKnob;`  
`addAndMakeVisible(driveKnob = new juce::Slider("Drive"));`

Does anyone have an idea of how to solve this?

---

<div class="post-metadata">

**Author:** ![connorreviere](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/connorreviere/32/4890_2.png) [@connorreviere](https://forum.juce.com/u/connorreviere)\
**Post date:** [September 6, 2020, 11:03pm UTC](https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099/10 "2020-09-06T23:03:53Z")

</div>

Assuming you’re using at least C++14, you should use `std::make_unique` to allocate a new `std::unique_ptr`. So your above snippet becomes:

```
std::unique_ptr<juce::Slider> driveKnob; 
driveKnob = std::make_unique<juce::Slider> ("Drive");
addAndMakeVisible (driveKnob.get()); // use get() whenever a raw pointer is needed
```

---

<div class="post-metadata">

**Author:** ![wesleylaurence](https://avatars.discourse-cdn.com/v4/letter/w/ba8739/32.png) [@wesleylaurence](https://forum.juce.com/u/wesleylaurence)\
**Post date:** [September 6, 2020, 11:24pm UTC](https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099/11 "2020-09-06T23:24:23Z")

</div>

Worked perfectly, thanks!

---

<div class="post-metadata">

**Author:** ![PluginPenguin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pluginpenguin/32/10203_2.png) [@PluginPenguin](https://forum.juce.com/u/PluginPenguin)\
**Post date:** [September 7, 2020, 8:33am UTC](https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099/12 "2020-09-07T08:33:04Z")

</div>

> [@connorreviere](#):
>
> `addAndMakeVisible (driveKnob.get()); // use get() whenever a raw pointer is needed`

Minor stile detail: While there is nothing wrong with it, I think the overload of `addAndMakeVisible` that takes a reference is even better to read here, as there is a suitable `operator*` overload to `std::unique_ptr`. Therefore, I use

```auto
addAndMakeVisible (*driveKnob);

```

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [September 7, 2020, 9:06am UTC](https://forum.juce.com/t/replacing-scopedpointer-with-std-unique-ptr/26099/13 "2020-09-07T09:06:26Z")

</div>

> [@connorreviere](#):
>
> Assuming you’re using at least C++14

Just for the records, thanks to a backport that the JUCE team did, std::make\_unique is also available if you use JUCE with C++11, it’s not necessary to have C++14 compliance for that specific feature to be available
