简体   繁体   English

可以将std :: unique_ptr用作参数吗?

[英]Can std::unique_ptr be used as an argument?

Now, I'm just studying about smart_ptr (unique_ptr / shared_ptr). 现在,我只是在研究smart_ptr(unique_ptr / shared_ptr)。 And I wrote test code like this for checking unique_ptr can be used as argument. 我写了这样的测试代码来检查unique_ptr可以用作参数。

typedef struct __T_TEST
{
char cT1;
char cT2;
} T_TEST;

int main(...)
{
...
char szB[2] = {0x61, 0x62};

auto pTest = std::make_unique<T_TEST>();
std::memcpy(pTest, szB, 2);
printf("T1 = [%02x]\n", pTest->cT1);
printf("T2 = [%02x]\n", pTest->cT2);

return 0;
}

And then, I added one function using unique_ptr as parameter like this. 然后,我添加了一个使用unique_ptr作为参数的函数,如下所示。

int TestPrint(std::unique_ptr<T_TEST> p_pData)
{
    printf("T1 in TestPrint Func = [%02x]\n", p_pData->cT1);
    printf("T2 in TestPrint Func = [%02x]\n", p_pData->cT2);
    return 0;
}

I'm just calling that function in main function after printf. 我只是在printf之后在main函数中调用该函数。 But it doesn't work. 但这是行不通的。

printf("T1 = [%02x]\n", pTest->cT1);
printf("T2 = [%02x]\n", pTest->cT2);

TestPrint(pTest);  <- Can't pass the pTest!

return 0;
}

In this case, I can't pass the pTest. 在这种情况下,我无法通过pTest。 Why not? 为什么不? I think pTest is std::unique_ptr and p_pData is std::unique_ptr in TestPrint function. 我认为在TestPrint函数中pTest是std :: unique_ptr,而p_pData是std :: unique_ptr。 So it's the same type!! 所以是同一类型! But I can't. 但是我不能。 I want to know why it doesn't work. 我想知道为什么它不起作用。

Of course, it can work after changing the function to std::unique_ptr<T_TEST>& p_pData . 当然,将函数更改为std::unique_ptr<T_TEST>& p_pData之后,它就可以工作。 But I don't understand why I can't pass the parameter despite of same data type. 但是我不明白为什么即使数据类型相同也无法传递参数。

std::unique_ptr is a move only type(cannot be copied), if you want to pass it in to the function as a parameter, you can std :: unique_ptr是仅移动类型(无法复制),如果要将其作为参数传递给函数,则可以

1 : pass by reference 1:通过引用

int TestPrint(std::unique_ptr<T_TEST> const &p_pData)
{
    printf("T1 in TestPrint Func = [%02x]\n", p_pData->cT1);
    printf("T2 in TestPrint Func = [%02x]\n", p_pData->cT2);
    return 0;
}

2 : move it 2:移动

TestPrint(std::move(pTest));

3 : do not use unique_ptr as parameter, use pointer 3:不使用unique_ptr作为参数,使用指针

int TestPrint(T_TEST *p_pData)
{
    printf("T1 in TestPrint Func = [%02x]\n", p_pData->cT1);
    printf("T2 in TestPrint Func = [%02x]\n", p_pData->cT2);
    return 0;
}

TestPrint(pTest.get());

Pointer is bad at resource management, but good at access resource, if you do not need to pass the ownership to the function, you can use pointer to replace unique_ptr, this is more flexible IMHO, because the raw pointer can work with shared_ptr, unique_ptr, weal_ptr and other smart pointer, but unique_ptr must work with unique_ptr only. 指针不利于资源管理,但擅长访问资源,如果不需要将所有权传递给函数,则可以使用指针来替换unique_ptr,这是更灵活的恕我直言,因为原始指针可以与shared_ptr,unique_ptr一起使用,weal_ptr和其他智能指针,但unique_ptr必须仅与unique_ptr一起使用。

In most cases, I only use smart pointer as a function parameter when I want to share(std::shared_ptr) or transfer ownership(std::unique_ptr). 在大多数情况下,我仅在要共享(std :: shared_ptr)或转让所有权(std :: unique_ptr)时才将智能指针用作函数参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM