How can I pass std::unique_ptr into a function

前端 未结 7 1066
忘了有多久
忘了有多久 2020-12-02 07:07

How can I pass a std::unique_ptr into a function? Lets say I have the following class:

class A
{
public:
    A(int val)
    {
        _val = val         


        
7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 07:55

    Why can I not pass a unique_ptr into a function?

    You can, but not by copy - because std::unique_ptr<> is not copy-constructible.

    Surely this is the primary purpose of the construct?

    Among other things, std::unique_ptr<> is designed to unequivocally mark unique ownership (as opposed to std::shared_ptr<> ).

    And most strangely of all, why is this an OK way of passing it?

    Because in that case, there is no copy-construction.

提交回复
热议问题