creating a shared_ptr from unique_ptr

后端 未结 3 843
北荒
北荒 2021-02-04 23:17

In a piece of code I reviewed lately, which compiled fine with g++-4.6, I encountered a strange try to create a std::shared_ptr from std::unique_

3条回答
  •  北海茫月
    2021-02-05 00:09

    Here's a reduced example which clang incorrectly compiles:

    struct ptr
    {
      int* p;
    
      explicit operator bool() const { return p != nullptr; }
    };
    
    int main()
    {
      ptr u{};
      int* p = new int(u);
    }
    

    Clang uses the explicit bool conversion operator to initialize the int (and the Intel compiler does too.)

    Clang 3.4 does not allow:

    int i = int(u);
    

    but it does allow:

    int* p = new int(u);
    

    I think both should be rejected. (Clang 3.3 and ICC allow both.)

    I've added this example to the bug report.

提交回复
热议问题