Why do we have reinterpret_cast in C++ when two chained static_cast can do its job?

后端 未结 7 1491
孤城傲影
孤城傲影 2020-12-01 01:58

Say I want to cast A* to char* and vice-versa, we have two choices (I mean, many of us think we\'ve two choices, because both seems to wor

7条回答
  •  无人及你
    2020-12-01 02:22

    A concrete example:

    char a[4] = "Hi\n";
    char* p = &a;
    
    f(reinterpret_cast(p));  // call f after restoring full type
          // ^-- any_cast<> can't do this...
    
    // e.g. given...
    template    // <=--- can match this function
    void f(T (&)[N]) { std::cout << "array size " << N << '\n'; }
    

提交回复
热议问题