Since C++11, because of several reasons, developers tend to use smart pointer classes for dynamic lifetime objects. And with those new smart pointer classes, standards, even suggest to not use operators like new
instead they suggest to use make_shared
or make_unique
to avoid some error prone.
If we like to use a smart pointer class, like shared_ptr
, we can construct one like,
shared_ptr<int> p(new int(12));
Also we would like to pass a custom deleter to smart pointer classes,
shared_ptr<int> p(new int(12), deleter);
On the other hand, if we like to use make_shared
to allocate, for ex. int
, instead of use new
and shared_ptr
constructor, like on the first expression above, we can use
auto ip = make_shared<int>(12);
But what if we like to also pass a custom deleter to make_shared
, is there a right way to do that? Seems like compilers, at least gcc, gives an error to,
auto ip = make_shared<int>(12, deleter);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…