Initializing container of unique_ptrs from initializer list fails with GCC 4.7

前端 未结 2 1480
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 04:36

I am trying to initialise an std::vector> in a way that is equivalent to an example from Bjarne Stroustrup\'s C++11 FAQ

2条回答
  •  我在风中等你
    2020-11-30 05:10

    After "fixing" your example:

    #include 
    #include 
    #include 
    
    int main()
    {
        std::vector> vs = { { new std::string{"Doug"} }, { new std::string{"Adams"} } }; // fails
        std::unique_ptr ps { new std::string{"42"} }; // OK
    }
    

    I got very a clear error message:

    error: converting to 'std::unique_ptr >' from initializer list would use explicit constructor 'std::unique_ptr<_Tp, _Dp>::unique_ptr(std::unique_ptr<_Tp, _Dp>::pointer) [with _Tp = std::basic_string, _Dp = std::default_delete >, std::unique_ptr<_Tp, _Dp>::pointer = std::basic_string*]'
    

    This error tells us that it is not possible to use the unique_ptr's explicit contructor!

提交回复
热议问题