8

std::unique_ptr uniquely controls the object it points to and, hence, does not utilize reference counting. A singleton ensures only one object may be created utilizing reference counting.

Would then std::unique_ptr perform identically to a singleton?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Mushy
  • 2,535
  • 10
  • 33
  • 54

4 Answers4

13

A singleton ensures only one instance of a type.

A unique_ptr ensures only one smart pointer to any instance.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
  • Your second statement is not necessarily true. – Preet Kukreti Apr 09 '13 at 15:59
  • @PreetKukreti I agree with your answer. I could be my own worst enemy and assert that my first statement isn't necessarily true either. You could leverage buggy code to duplicate either. Consider my use of "ensures" to mean "takes measures to help ensure". *Perhaps I should edit...* – Drew Dormann Apr 09 '13 at 16:02
  • 1
    no worries, I voted you up anyway, you describe the general idea quite succinctly :) – Preet Kukreti Apr 09 '13 at 16:05
6

Would then std::unique_ptr perform identically to a singleton?

No. Let's say we have class Foo that's intended to be a singleton. Using a typical singleton pattern, there's no way to construct more than one Foo.

Having a std::unique_ptr<Foo> means there will be one pointer to a particular instance of Foo, but that doesn't prevent the creation of other instances of Foo (either with other unique_ptrs or with raw pointers to local variables). Thus Foo, wouldn't be a singleton.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
4

std::unique_ptr achieves single ownership semantics by only providing a move constructor and no copy constructor or assignment operator.

It is not a case of singleton at all, since you can have multiple unique_ptrs referencing different instances of the same type. A singleton doesn't let you construct the type directly, but provides an accessor that manages a sole instance.

Also, Drew's assertion that

"A unique_ptr ensures only one smart pointer to any instance."

is false. If you simply do:

T* nt = new T;
std::unique_ptr<T> up1(nt);
std::unique_ptr<T> up2(nt);

then you have two unique pointers owning the same resource - and you will only notice a problem at run time, not compile time. Of course, this is incorrect usage of unique_ptr, but this reinforces that a unique_ptr does not ensure you anything, it is simply a pointer container that holds sole ownership from its own perspective, and through its api, makes it hard to accidentally create temporary copies.

Furthermore, you can have other (smart) pointer types pointing to the same raw pointer/resource independently of any unique_ptr. It is completely up to the using code to define ownership and lifetime policies of its resources and smart pointer instances

Preet Kukreti
  • 8,417
  • 28
  • 36
0

Correct me if I'm wrong, but as far as I remember a singelton is a class which can only have one instance. That's completely different. So no.

didierc
  • 14,572
  • 3
  • 32
  • 52
JMRC
  • 1,473
  • 1
  • 17
  • 36