简体   繁体   中英

Using a shared_ptr<const T> from a stored shared_ptr<T>

I have some data stored using an std::shared_ptr<T> .

Now I want to use that that as const input to a function, so I need an std::shared_ptr<const T> . How do I do that?

Can I pass the std::shared_ptr<T> object to the function expecting a std::shared_ptr<const T> ?

Yes .

#include <memory>

void foo(std::shared_ptr<const int> ptr) {}

int main()
{
    auto ptr = std::make_shared<int>();
    foo(ptr);
}

Why didn't you just try it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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