Why doesn't `unique_ptr` degrade to `QByteArray*`?

后端 未结 1 517
温柔的废话
温柔的废话 2021-01-29 15:04

I have the following code:

  msg_buf_ptr = std::make_unique();

  return QDataStream{msg_buf_ptr, QIODevice::WriteOnly};

I am

相关标签:
1条回答
  • 2021-01-29 15:42

    No, this is not a special case; the standard-library smart pointers do not degrade implicitly in contexts requiring raw pointers.

    As mentioned in the question, the proper way to access the underlying raw pointer from a unique_ptr is to use get(). This is a design feature, apparently intended to help avoid accidentally causing multiple-ownership scenarios, which would lead to undefined behavior.

    0 讨论(0)
提交回复
热议问题