7

In his 2014 CppCon talk, presenter Jon Kalb shows the following function call:

FooBar( smart_ptr<Foo>( new Foo( f ) ),
        smart_ptr<Bar>( new Bar( b ) ) );

Under the heading "Smart Pointer 'Gotcha'" he concludes, that this is not exception-safe. Due to the evaluation of function arguments being unordered and unsequenced, there is a window of opportunity, where an exception can cause resources to leak.

While I believe that this was correct at the time the statement was made, I don't think this is still true in C++17, where the evaluation of function arguments is still unordered, yet sequenced with respect to each other.

Question: Is this correct, and if so, is the code posted above exception-safe in C++17?

IInspectable
  • 35,521
  • 8
  • 69
  • 148
  • in herbs gotw that seems to be uptodate for C++14 he has a similar example: https://herbsutter.com/2013/05/29/gotw-89-solution-smart-pointers/ – 463035818_is_not_a_number Sep 28 '17 at 14:39
  • Not sure this is answered in the dupe, but yes you're correct - in C++14, you could have interleaving, but in C++17, you can't. – Barry Sep 28 '17 at 14:45
  • @Barry - The answer in the dupe touches upon it at the end – StoryTeller - Unslander Monica Sep 28 '17 at 14:47
  • @Barry: It is indeed addressed in the duplicate, albeit as an edit, buried way down, underneath a wall of text. While I came across that question in my research, I didn't spot it immediately. I still believe that my question is more specific than the duplicate. And while this question apparently still attracts [incorrect answers](https://stackoverflow.com/a/46471889/1889329), it may just be valuable in and of itself. – IInspectable Sep 28 '17 at 14:51
  • @IInspectable Added a new answer to that question that is hopefully more explicit? – Barry Sep 28 '17 at 15:05
  • @IInspectable - It's very valuable. Dupes are an important feature of SO, as they help point to the holistic answers already given elsewhere. – StoryTeller - Unslander Monica Sep 28 '17 at 18:33
  • 1
    @Barry: That answer perfectly answers my question, but doesn't fully address the question for which it was posted as an answer. I'm voting to re-open this question, so that you can post your answer here, in case that goes through. As it stands closing this question, and providing an answer to it to a related question, that has no link back to the question it is trying to answer, doesn't improve the overall quality of this site. – IInspectable Sep 29 '17 at 08:12
  • @IInspectable It doesn't have to fully address it, it just has to be useful. This question is still a dupe of that one though - it's more useful to just have the one "C++17 evaluation order" question. Can just keep adding answers to that one to make it more complete over time. – Barry Sep 29 '17 at 13:12

0 Answers0