0

I can't see any point to using the copy-and-swap idiom in C++11. If I need a handle, I can use unique_ptr or shared_ptr. If I need a collection of objects, I can just vector or string.

struct Relax
{
   shared_ptr<Texture> resource;
public:
   /* rest of stuff */
};

To me, the copy-and-swap idiom sounds like a pointless thought exercise full of bugs waiting to happen.

  • worry about exceptions

  • do I need to worry about self-assignment or not? premature optimization is the root of all evil

  • did I miss a place to call swap somewhere in my class's constructors?

  • maybe I forgot a delete somewhere?

Let's say I have a compelling reason to use manual memory management. There's still allocators, swapping pointers, etc.

In modern C++, when would I need to do manual memory management (new, delete) w.r.t copy-and-swap?

  • 8
    dup of : http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom ? – marcinj Jun 09 '16 at 11:21
  • 5
    Sounds to me that you don't know what the copy-and-swap idiom **is**. – Cheers and hth. - Alf Jun 09 '16 at 11:22
  • See [this](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom?rq=1): it's the very first point what CAS is for. – edmz Jun 09 '16 at 11:22
  • 3
    @user6445047 You're expected to do the bare amount of research **before** asking a question. – John Cena Jun 09 '16 at 11:25
  • "when would I need to do manual memory management" Why are you assuming that you do? Did you try googling it before coming to stackoverflow? – uh oh somebody needs a pupper Jun 09 '16 at 11:30
  • @Cheersandhth.-Alf Y'know maybe I'm missing something, but that question clearly shows manual resource management as well as everything else OP pointed in his question that he found pointless. Maybe you misread the question? – user6412786 Jun 09 '16 at 11:35

0 Answers0