0

次のコードがあります

#include <iostream>
#include <memory>
#include <cassert>

int main()
{
    void* p_any = nullptr;

    {
        auto  p_src = std::make_shared<int>(10); // new instance        
        p_any = p_src.get();                     // get raw unmanaged pointer?
        auto  p_again = reinterpret_cast<int*>(p_any);
        assert(*p_src == *p_again);
    }

    auto  p_again = reinterpret_cast<int*>(p_any); // ??
    std::cout << *p_again <<  "\n";                // undefined?, expected?

}

最後の 2 つのステートメントは安全ですか?

「10」を出力せずにhttp://cpp.sh/6pohを実行できますが、期待どおりですか? または単に未定義の動作ですか?

4

1 に答える 1