# ScopedPointer semantics, STL

**URL:** <https://forum.juce.com/t/scopedpointer-semantics-stl/6551>\
**Category:** General JUCE discussion\
**Created:** [March 11, 2011, 1:15am UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551 "2011-03-11T01:15:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![madronalabs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/madronalabs/32/192_2.png) [@madronalabs](https://forum.juce.com/u/madronalabs)\
**Post date:** [March 11, 2011, 1:15am UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/1 "2011-03-11T01:15:33Z")

</div>

I’m making a container full of Drawables that I can access by name. In the Juce demo, there’s an example of something similar, but it takes a couple of data structures, like this (edited for clarity):

```auto
StringArray iconNames;
OwnedArray <Drawable> icons;
// ...

// create a named drawable
iconNames.add ("myName");
icons.add (Drawable::createFromImageDataStream (*svgFileStream));

 // fetch a named drawable
Drawable* image = icons [iconNames.indexOf ("myName")]->createCopy();
```

Using std::tr1::shared\_ptr and STL containers instead, doing the same thing looks like

```auto
typedef std::tr1::shared_ptr<Drawable> DrawablePtr;
std::map <String, DrawablePtr> icons;
// ...

// create a named drawable
icons["myName"] = (Drawable::createFromImageDataStream (*svgFileStream));

// fetch a named drawable
Drawable* image = icons["myName"];
```

With the STL method there is no need to mess around with two data structures that could possibly get out of sync. The std::map code also has the advantages of working with any kind of sortable object you want (such as a hash) for a key, and of being highly optimized.

JUCE’s ScopedPointers are by design not copyable, a quirk that is documented but can lead to some crazy-making situations if you are unaware (me earlier today.) For example, this compiles OK

```auto
ScopedPointer<Drawable> p;
p = (Drawable::createFromImageData(data, dataSize));
```

but this doesn’t.

```auto
ScopedPointer<Drawable> p = (Drawable::createFromImageData(data, dataSize));
```

For the same reason, ScopedPointers can’t be contained in STL containers. Since Jules is a busy guy, and JUCE has its own set of container classes, I’m not saying this should be changed. But if like me, you rely on both JUCE and the STL, take note: there are some gotchas.

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [March 11, 2011, 3:19am UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/2 "2011-03-11T03:19:07Z")

</div>

Gotta be honest, I’ve tried it all ways and I prefer the Juce way (or my own home-brew).

When I am in a hurry to implement code, yeah I’ll use a std::vector or a shared\_ptr just to whip something out fast and see if it works but when I write it “for real” I will use something else like a juce::Array or ReferenceCountedObject, etc…

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [March 11, 2011, 3:20am UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/3 "2011-03-11T03:20:26Z")

</div>

[http://www.rawmaterialsoftware.com/juce.php](http://www.rawmaterialsoftware.com/juce.php)

> [@Jules](#):
>
> In designing JUCE, I’ve tried to make it:  
> …  
> “Modern C++ - very object-oriented, avoiding older C-style coding techniques wherever possible. But pushing the limits of the language is not what this library is about - you won’t find any elaborate intertwined templates that require genius-level code-breaking skills to understand.”

---

<div class="post-metadata">

**Author:** ![ckk](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@ckk](https://forum.juce.com/u/ckk)\
**Post date:** [March 11, 2011, 6:12am UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/4 "2011-03-11T06:12:44Z")

</div>

> [@madronalabs](#):
>
> JUCE’s ScopedPointers are by design not copyable, a quirk that is documented but can lead to some crazy-making situations if you are unaware (me earlier today.) For example, this compiles OK
> 
> ```auto
> ScopedPointer<Drawable> p;
> p = (Drawable::createFromImageData(data, dataSize));
> ```
> 
> but this doesn’t.
> 
> ```auto
> ScopedPointer<Drawable> p = (Drawable::createFromImageData(data, dataSize));
> ```

It doesn’t on Mac, and probably Linux (never tried). On Windows it works.  
It also isn’t, much a problem since you can write the following instead:

```auto
ScopedPointer<Drawable> p(Drawable::createFromImageData(data, dataSize));
```

Chris

---

<div class="post-metadata">

**Author:** ![madronalabs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/madronalabs/32/192_2.png) [@madronalabs](https://forum.juce.com/u/madronalabs)\
**Post date:** [March 11, 2011, 6:43am UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/5 "2011-03-11T06:43:38Z")

</div>

> [@ckk](#):
>
> It doesn’t on Mac, and probably Linux (never tried). On Windows it works.  
> It also isn’t, much a problem since you can write the following instead:

No, it’s not really a problem as far as getting things done. But it was confusing to me. So I made this topic here—I hope it helps some other confused person who searches on ScopedPointer or STL.

---

<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:** [March 11, 2011, 9:30am UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/6 "2011-03-11T09:30:09Z")

</div>

You’re welcome to mix std containers with juce stuff - my intention was never to replace them or ban them, just to provide some container types that were more to my own taste, as I’ve never been a fan of the naming conventions or style of the std ones, and there are some tasks where they can be a bit of a pain. There’s not really much point comparing the std::map class though, because juce doesn’t have one of those… if I wrote one, it’d presumably produce just as good a result as your example with the std::map.

Re: this…

```auto
ScopedPointer<Drawable> p = Drawable::createFromImageData(data, dataSize);
```

…yes, it’s just a fact of life in c++ that you can’t write that. If you use std::auto\_ptr, you’ll find it behaves exactly the same way. Any kind of single-ownership pointer class must use a copy constructor with a non-const rhs, because it modifies the thing it is copying from. That means the compiler can’t automatically convert the statement into the correct form of:

```auto
ScopedPointer<Drawable> p (Drawable::createFromImageData(data, dataSize));
```

But IMHO (and in the humble opinion of all the c++ gurus that I’ve read too) you should _always_ write your non-primitive type declarations like this anyway. You don’t _really_ want the compiler to call the object’s default constructor, then call its operator= method as two separate steps, do you? So why write it like that and hope that the compiler will sort it out for you?

---

<div class="post-metadata">

**Author:** ![X-Ryl669](https://avatars.discourse-cdn.com/v4/letter/x/a5b964/32.png) [@X-Ryl669](https://forum.juce.com/u/X-Ryl669)\
**Post date:** [March 11, 2011, 5:31pm UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/7 "2011-03-11T17:31:29Z")

</div>

> [@jules](#):
>
> But IMHO (and in the humble opinion of all the c++ gurus that I’ve read too) you should _always_ write your non-primitive type declarations like this anyway. You don’t _really_ want the compiler to call the object’s default constructor, then call its operator= method as two separate steps, do you? So why write it like that and hope that the compiler will sort it out for you?

Because the standard doesn’t say it’ll act like you’re saying, it even says the opposite, it’ll act like the original poster says:

```auto
From ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §8.5 Initializers [dcl.init] para. 12
Class a = b; // Call the constructor of Class(taking a b as parameter if any)
```

What you’re saying is only valid if there is no accessible copy constructor (like a private one), like most Juce classes does, but it’s not the rule, it’s merely the exception I would say.  
However, don’t make a rule out of the few, as if you force newbies writing code like you say, they’ll hit the issues shown below.

Using “Class a();” like code leads to:  
[http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.2](http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.2)  
and  
[http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.21](http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.21)

---

<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:** [March 11, 2011, 5:50pm UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/8 "2011-03-11T17:50:54Z")

</div>

> [@](#):
>
> However, don’t make a rule out of the few, as if you force newbies writing code like you say, they’ll hit the issues shown below.

No, that’s not what I said at all - I would never write a no-parameters constructor like that, and would not encourage anyone else to do so.

I’m only talking about constructors that take one parameter. You should write a 1-param constructor with the same syntax you’d use if it took 2 or more params. It’s the same number of keystrokes, you can’t argue that it’s any quicker to write it as an assignment!

---

<div class="post-metadata">

**Author:** ![madronalabs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/madronalabs/32/192_2.png) [@madronalabs](https://forum.juce.com/u/madronalabs)\
**Post date:** [March 11, 2011, 7:28pm UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/9 "2011-03-11T19:28:57Z")

</div>

```auto
But IMHO (and in the humble opinion of all the c++ gurus that I've read too) you should *always* write your non-primitive type declarations like this anyway. You don't *really* want the compiler to call the object's default constructor, then call its operator= method as two separate steps, do you? So why write it like that and hope that the compiler will sort it out for you?
```

Point well taken. I tend to use = here because I find it more readable—probably a C coding habit I should break.

Yes I follow you about auto\_ptr implementations and I hope I made it clear I don’t think this is a juce problem. Rather than using auto\_ptr, I either try to use objects directly, or shared\_ptr when I need to put something into a container.

Like C++ in general, STL can be a mine field for the unwary. Some parts are excessively complex, but the useful parts are very well designed and very efficient. I find the [Google C++ style guide](http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml) to be a great introduction to these parts.

I brought up std::map because I need it. Keeping maps of (hashed) names is a great tool for managing medium to large systems effectively. The comparison explains to someone reading why I would want to use STL and std::map in the first place. I don’t think there needs to be an equivalent in Juce.

---

<div class="post-metadata">

**Author:** ![X-Ryl669](https://avatars.discourse-cdn.com/v4/letter/x/a5b964/32.png) [@X-Ryl669](https://forum.juce.com/u/X-Ryl669)\
**Post date:** [March 11, 2011, 7:30pm UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/10 "2011-03-11T19:30:13Z")

</div>

No, for sure it’s the same. It’s just that, as soon as a newB start seeing

```auto
Class a( param);
```

They might start using the same syntax everywhere, and then, start writing:

```auto
Class a();
```

And get a really really strange error.  
That said, that’s how we all learn, so it might worth a note somewhere to save some time.

---

<div class="post-metadata">

**Author:** ![kunitoki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kunitoki/32/15_2.png) [@kunitoki](https://forum.juce.com/u/kunitoki)\
**Post date:** [March 13, 2011, 2:31pm UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/11 "2011-03-13T14:31:01Z")

</div>

> [@](#):
>
> With the STL method there is no need to mess around with two data structures that could possibly get out of sync. The std::map code also has the advantages of working with any kind of sortable object you want (such as a hash) for a key, and of being highly optimized.

Here is the Hash and OwnedHash classes in perfectly 100% JUCE style (took me a while!) that was never accepted in the main development trunk by Jules.

This is designed for basic types (like Array):  
[http://code.google.com/p/juced/source/browse/trunk/juce/src/extended/containers/jucetice\_Hash.h](http://code.google.com/p/juced/source/browse/trunk/juce/src/extended/containers/jucetice_Hash.h)

This is specially crafted for owning objects pointers (like OwnedArray):  
[http://code.google.com/p/juced/source/browse/trunk/juce/src/extended/containers/jucetice\_OwnedHash.h](http://code.google.com/p/juced/source/browse/trunk/juce/src/extended/containers/jucetice_OwnedHash.h)

Hope these helps !

---

<div class="post-metadata">

**Author:** ![madronalabs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/madronalabs/32/192_2.png) [@madronalabs](https://forum.juce.com/u/madronalabs)\
**Post date:** [March 15, 2011, 5:44pm UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/12 "2011-03-15T17:44:00Z")

</div>

Cool stuff, kraken, thanks for sharing.

---

<div class="post-metadata">

**Author:** ![X-Ryl669](https://avatars.discourse-cdn.com/v4/letter/x/a5b964/32.png) [@X-Ryl669](https://forum.juce.com/u/X-Ryl669)\
**Post date:** [March 15, 2011, 5:54pm UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/13 "2011-03-15T17:54:03Z")

</div>

Jules, if you still monitor this thread, these classes are really nice looking, and might worth inclusion, don’t you think ?

---

<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:** [March 15, 2011, 6:13pm UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/14 "2011-03-15T18:13:28Z")

</div>

Sorry, I hadn’t followed the end of this thread - yes, they do look like really nice classes! Kraken, would you be happy to let me take them over and add them to the library?

---

<div class="post-metadata">

**Author:** ![kunitoki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kunitoki/32/15_2.png) [@kunitoki](https://forum.juce.com/u/kunitoki)\
**Post date:** [March 16, 2011, 11:30am UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/15 "2011-03-16T11:30:16Z")

</div>

> [@jules](#):
>
> Sorry, I hadn’t followed the end of this thread - yes, they do look like really nice classes! Kraken, would you be happy to let me take them over and add them to the library?

Yeah sure, that was the original purpose of those writings in (quite) perfect juce style 🙂

feel free to add those classes in the framework, i’m very happy i don’t have to manage to include my own ones all the time !

PS. one thing that can be optimized is the usage of `const KeyType&` instead of `const KeyType` in all functions taking the key, allowing a faster lookup by const reference (especially when used with strings).

---

<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:** [March 16, 2011, 11:42am UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/16 "2011-03-16T11:42:30Z")

</div>

[quote]Yeah sure, that was the original purpose of those writings in (quite) perfect juce style

feel free to add those classes in the framework, i’m very happy i don’t have to manage to include my own ones all the time ![/quote]

Cool, much appreciated, I’ll take a look through very soon! Sorry if you’ve posted them before and I failed to notice/remember… it’s hard to keep on top of all the forum activity sometimes!

---

<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:** [March 16, 2011, 7:02pm UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/17 "2011-03-16T19:02:05Z")

</div>

Ok, I’ve added a hash-map class. I started with your code, but I think by the time I’d finished tweaking it, the result contains absolutely none of the original code at all! Might be interesting for you to compare-and-contrast though…!

---

<div class="post-metadata">

**Author:** ![kunitoki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kunitoki/32/15_2.png) [@kunitoki](https://forum.juce.com/u/kunitoki)\
**Post date:** [March 17, 2011, 11:37am UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/18 "2011-03-17T11:37:28Z")

</div>

Interesting. What i don’t know if it can be optimized is that all the accessors/manipulation functions of the map take a KeyTypeParameter by copy, and when using strings that holds the keys, there will be much copying around and a general overhead when asking for a value.

What about using a const KeyTypeParameter& instead ? (or i don’t get what’s the PARAMETER\_TYPE macro for ?)

---

<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:** [March 17, 2011, 12:14pm UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/19 "2011-03-17T12:14:35Z")

</div>

The PARAMETER\_TYPE macro provides the best version of the type for use as a parameter - so if your type is a MyObject, then PARAMETER\_TYPE (MyObject) == “const MyObject&”, but PARAMETER\_TYPE (int) == “int”.

---

<div class="post-metadata">

**Author:** ![kunitoki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kunitoki/32/15_2.png) [@kunitoki](https://forum.juce.com/u/kunitoki)\
**Post date:** [March 17, 2011, 9:48pm UTC](https://forum.juce.com/t/scopedpointer-semantics-stl/6551/20 "2011-03-17T21:48:46Z")

</div>

it’s in juce ? cunning trick !!!

[Next page](https://forum.juce.com/t/scopedpointer-semantics-stl/6551.md?page=2)
