1

When I write

std::shared_ptr<MyType> x;

x is initialized with an empty shared pointer to a nullptr. But I would like it to automatically call the default (or some other designated constructor) of MyType. I know, that I could just write:

std::shared_ptr<MyType> x = std::maked_shared<MyTYpe>();

but sometimes people forget and then you run into trouble, so enforcing this by the compiler would be nice.

YesThatIsMyName
  • 1,585
  • 3
  • 23
  • 30
Jonn Dove
  • 477
  • 2
  • 11
  • 4
    It's like asking `int *p;` to automatically allocate an integer. Not sure that would be a good idea. – Marco A. May 11 '18 at 07:51
  • Possible duplicate of [Initialising shared\_ptr , when the object requires a default constructor](https://stackoverflow.com/questions/17189566/initialising-shared-ptr-when-the-object-requires-a-default-constructor) – YesThatIsMyName May 11 '18 at 07:52
  • 4
    No. You'll have to make your own shared pointer class, perhaps call it `never_null_shared_ptr`, and perhaps use an underlying `std::shared_ptr` for its implementation. – Benjamin Lindley May 11 '18 at 07:54
  • @MarcoA., in general no, but I only want it for MyType, which is special. – Jonn Dove May 11 '18 at 07:55
  • @JonnDove then I agree with Bejamin: roll your own type. – Marco A. May 11 '18 at 07:55
  • @YesThatIsMyName: No. That's not related to his problem at all, other than being about shared pointers. – Benjamin Lindley May 11 '18 at 07:56
  • @YesThatIsMyName, this thread only gives the solution, I am explictly not looking for. – Jonn Dove May 11 '18 at 07:56
  • If you're only interested by `std::shared_ptr`, you could specialize `std::shared_ptr::shared_ptr()`. But it may come to a surprise to your colleagues and increase the number of WTF/line of your codebase. Plus possibly break something else. Not A Good Idea (TM). – YSC May 11 '18 at 07:58
  • 6
    Just think about this a bit more: you're asking to make `shared_ptr` behave not like `shared_ptr`. That's obviously not going to be possible. It would break valid templated code which uses `shared_ptr`, when `T` is your special type, which assumes that `shared_ptr` behaves as specified in the standard. –  May 11 '18 at 07:58
  • 2
    @YSC That may compile but worth addressing is that it has undefined behaviour as far as standard C++ is concerned. –  May 11 '18 at 07:59
  • 2
    if the smart pointer should never have a null value, why do you use a smartpointer in the first place? Cant you use a reference (or simply the object itself) ? – 463035818_is_not_an_ai May 11 '18 at 08:27
  • 1
    @user463035818: default constructed is different than never null. I would like to have that for pimpl idiom (with `const std::unique_ptr`)... (but as it would require definition of `pimpl`, that concept is so caducous). – Jarod42 May 11 '18 at 08:52

1 Answers1

7

You can create a class, something like:

template <typename T>
struct defaulted_shared_ptr : std::shared_ptr<T>
{
    using std::shared_ptr<T>::shared_ptr;
    defaulted_shared_ptr() : std::shared_ptr<T>(std::make_shared<T>()) {}
};

Then

defaulted_shared_ptr<Foo> foo;

would contain a default contructed Foo

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302