从 C++ 中的共享指针获取对象类型

Mat*_*4Us 2 c++ types pointers object shared-ptr

有没有办法从共享指针获取对象类型?认为:

auto p = std::make_shared<std::string>("HELLO");
Run Code Online (Sandbox Code Playgroud)

我想从 p 中获取字符串类型,例如:

p::element_type s = std::string("HELLO");
Run Code Online (Sandbox Code Playgroud)

含义: p::element_type 是 std::string。

谢谢!

Jon*_*ter 5

shared_ptr::element_typeshared_ptr为您提供;所持有的类型 p您可以使用说明符访问此类型decltype。例如:

int main()
{
    auto p = std::make_shared<std::string>("HELLO");
    decltype(p)::element_type t = "WORLD"; // t is a std::string
    std::cout << *p << " " << t << std::endl;
}
Run Code Online (Sandbox Code Playgroud)