Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > it.comp.lang.c++ > #4739

STL library: copia e spostamento

Newsgroups it.comp.lang.c++
Date 2019-10-07 05:47 -0700
Message-ID <261e028c-589a-40f9-9e88-e89b3911964c@googlegroups.com> (permalink)
Subject STL library: copia e spostamento
From _merlinO_ <emanuele.merlo@gmail.com>

Show all headers | View raw


Come ho scritto nel post poco più sotto sto facendo uno studio sulle collezioni della STL library. Tutto è partito dall'esigenza di spostare oggetti tra liste diverse (delle stesso tipo), avendo sempre e comunque un'istanza unica e univoca dell'elemento (niente copie e duplicati).

Per esempio: Abbiamo una classe Foo che contiene all'interno una semplice stringa "messaggio". I costruttori e operatori per copia e spostamento sono tutti implementati. Che differenze vi aspettate tra questo codice:

  typedef std::list<Foo> foo_list;

  foo_list collection1 = {
    std::move(Foo{"Hello"}),
    std::move(Foo{"collections"}),
    std::move(Foo{"!!"})
  };

  foo_list collection2 = std::move(collection1);


e questo?

  typedef std::list<std::shared_ptr<Foo>> foo_list;

  foo_list collection1 = {
    std::make_shared<Foo>("Hello"),
    std::make_shared<Foo>("collections"),
    std::make_shared<Foo>("!!"),
  };

  foo_list collection2 = std::move(collection1);

Back to it.comp.lang.c++ | Previous | NextNext in thread | Find similar


Thread

STL library: copia e spostamento _merlinO_ <emanuele.merlo@gmail.com> - 2019-10-07 05:47 -0700
  Re: STL library: copia e spostamento enoquick <enoquick@gmail.com> - 2019-10-13 12:44 -0500
    Re: STL library: copia e spostamento _merlinO_ <emanuele.merlo@gmail.com> - 2019-10-17 03:00 -0700

csiph-web