0

I was going through Item 17 of Effective C++ 3rd Edition.

In that it is mentioned that order in which arguments are executed for a function call:

processWidget(std::tr1::shared_ptr<Widget> pw(new Widget), priority());

can be:

1) Execute "new Widget" 2) calling priority 3) Call tr1::shared_ptr c'tor.

Is this really the case in latest C++ compilers. I mean, i strongly feel that compiler should call step 1 and 3 one after the other not like above case since its one single argument. Please correct me if my understanding is wrong. Also, if someone asked this question, you can redirect it to that page since i didn't find any in SO.

  • 1
    It's not the case in C++17. – Passer By Mar 16 '19 at 06:09
  • 1
    Good reading: [Why doesn't c++ have a specified order for evaluating function arguements?](https://stackoverflow.com/questions/38764793/why-doesnt-c-have-a-specified-order-for-evaluating-function-arguements) – user4581301 Mar 16 '19 at 06:09
  • Took me longer than it should have to track this one down. [What are the evaluation order guarantees introduced by C++17?](https://stackoverflow.com/questions/38501587/what-are-the-evaluation-order-guarantees-introduced-by-c17) – user4581301 Mar 16 '19 at 06:14
  • @PasserBy so can i say there will be no memory leak then? or I don't have to add standalone statement before calling a function as mention in my attached link? – facebook-100001358991487 Mar 16 '19 at 06:18
  • Sidenote: make this all irrelevant [with `std::make_shared`](https://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared). – user4581301 Mar 16 '19 at 06:20
  • It's not about the latest compilers but rather that you enable C++17. If you use the newest compiler and don't set it to compile in C++17, there's no such guarantee. – Mirko Mar 16 '19 at 06:49
  • Yes. But the semantics is often misunderstood. Note also that `std::make_shared` does more than prevent memory leaks pre-C++17, it lumps allocations together, which isn't always the best option. – Passer By Mar 16 '19 at 07:01

0 Answers0