• Create Account

### #ActualKylotan

Posted 22 February 2013 - 06:23 AM

Well thank you very much.

Also of note, I've learned quite a bit more about smart pointers since I wrote this: http://agentultra.com/sdl-and-modern-c-plus-plus-images.html

One thing to be careful about when passing unique_ptr objects into function calls as I do in the examples above is: argument evaluation order. Some languages have a defined order in which to evaluate function arguments. C++ does not. Here it's probably not an issue but imagine a function that takes a unique_ptr and a couple of other values:

void myFunc(std::unique_ptr<MyType> x, int y)
{
...
}

// and then later on we call it like so...

myFunc(std::unique_ptr<MyType>(new MyType()), computeY());


What do you think will happen here if computeY throws an exception?

The answer: who knows? However depending on the order that the run-time chooses to evaluate the arguments in we could leave a dangling reference on the heap to that shiny, new MyType. Scott Meyers recommends (and I whole-heartedly agree) that you should declare your smart pointers in their own expression to avoid such awkward situations.

std::unique_ptr<MyType> my_ptr(new MyType());
myFunc(std::move(my_ptr), computeY());


Will work. Just look up Item 17 in Effective C++. You do already have three copies of that book, right? One for beside your bed, one for your desk, and one in the bathroom? If you want to program your games in C++ then you should have at least one copy. It's indispensable.

Anyway, my code is getting a little better. My 'engine' is coming along (if you can call it that). I'll post more tutorials as I come up with them.

: Fixed formatting presumably mangled by the notorious editor

### #3Kylotan

Posted 22 February 2013 - 06:23 AM

Well thank you very much.

Also of note, I've learned quite a bit more about smart pointers since I wrote this:&nbsp;http://agentultra.com/sdl-and-modern-c-plus-plus-images.html

One thing to be careful about when passing unique_ptr objects into function calls as I do in the examples above is: argument evaluation order. Some languages have a defined order in which to evaluate function arguments. C++ does not. Here it's probably not an issue but imagine a function that takes a unique_ptr and a couple of other values:

void myFunc(std::unique_ptr<MyType> x, int y)
{
...
}

// and then later on we call it like so...

myFunc(std::unique_ptr<MyType>(new MyType()), computeY());


What do you think will happen here if computeY throws an exception?

The answer: who knows? However depending on the order that the run-time chooses to evaluate the arguments in we could leave a dangling reference on the heap to that shiny, new MyType. Scott Meyers recommends (and I whole-heartedly agree) that you should declare your smart pointers in their own expression to avoid such awkward situations.

std::unique_ptr<MyType> my_ptr(new MyType());
myFunc(std::move(my_ptr), computeY());


Will work. Just look up Item 17 in Effective C++. You do already have three copies of that book, right? One for beside your bed, one for your desk, and one in the bathroom? If you want to program your games in C++ then you should have at least one copy. It's indispensable.

Anyway, my code is getting a little better. My 'engine' is coming along (if you can call it that). I'll post more tutorials as I come up with them.

: Fixed formatting presumably mangled by the notorious editor

### #2agentultra

Posted 21 February 2013 - 10:33 PM

<p>Well thank you very much. <img class="bbc_emoticon" src="http://public.gamedev.net//public/style_emoticons/default/smile.png" title=":)" /></p>
<p>&nbsp;</p>
<p>Also of note, I've learned quite a bit more about smart pointers since I wrote this:&nbsp;http://agentultra.com/sdl-and-modern-c-plus-plus-images.html</p>
<p>&nbsp;</p>
<p>One thing to be careful about when passing unique_ptr objects into function calls as I do in the examples above is:&nbsp;<em>argument evaluation order</em>. Some languages have a defined order in which to evaluate function arguments. C++ does not. Here it's probably not an issue but imagine a function that takes a unique_ptr and a couple of other values:</p>
<p>&nbsp;</p>
<pre class="_prettyXprint _linenums:1">
void myFunc(std::unique_ptr&lt;MyType&gt; x, int y)
{
...
}

// and then later on we call it like so...

myFunc(std::unique_ptr&lt;MyType&gt;(new MyType()), computeY());
</pre>
<p>&nbsp;</p>
<p>What do you think will happen here if computeY throws an exception?</p>
<p>&nbsp;</p>
<p>The answer:&nbsp;<em>who knows?</em> However depending on the order that the run-time chooses to evaluate the arguments in we could leave a dangling reference on the heap to that shiny, new MyType. Scott Meyers recommends (and I whole-heartedly agree) that you should declare your smart pointers in their own expression to avoid such awkward situations.</p>
<pre class="_prettyXprint _linenums:1">
std::unique_ptr&lt;MyType&gt; my_ptr(new MyType());
myFunc(std::move(my_ptr), computeY());
</pre>
<p>&nbsp;</p>
<p>Will work. Just look up Item 17 in&nbsp;<em>Effective C++</em>. You do already have three copies of that book, right? One for beside your bed, one for your desk, and one in the bathroom? If you want to program your games in C++ then you should have at least one copy. It's indispensable.</p>
<p>&nbsp;</p>
<p>Anyway, my code is getting a little better. My &quot;engine&quot; is coming along (if you can call it that). I'll post more tutorials as I come up with them.</p>

### #1agentultra

Posted 21 February 2013 - 08:57 PM

Well thank you very much.

PARTNERS