unique_ptr: is there problems with doing this

closed account (G30GNwbp)
Is there any potential problems with using unique_ptr where a raw pointer is expected?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void bar (int* i)
{
    *i *= *i;
}

int main()
{
    std::unique_ptr<int> up{new int};

    *up = 6;

    bar(&(*up));

    std::cout << *up  << std::endl;
   
    return 0;

}
Last edited on
Is there any potential problems with using unique_ptr where a raw pointer is expected?

I think your question is supposed to mean: "is this code OK"?
Technically speaking, yes it is.
But from a style point of view, no it isn't.

http://www.cplusplus.com/reference/memory/unique_ptr/get/

1
2
3
4
5
6
7
8
int main()
{
    // ...

    bar(up.get());

    // ...
}
closed account (G30GNwbp)
Thank you for a complete answer.
Topic archived. No new replies allowed.