相关疑难解决方法(0)

std :: shared_ptr和初始化列表

std :: shared_ptr构造函数的行为不符合我的预期:

#include <iostream>
#include <vector>

void func(std::vector<std::string> strings)
{
    for (auto const& string : strings)
    {
        std::cout << string << '\n';
    }
}

struct Func
{
    Func(std::vector<std::string> strings)
    {
        for (auto& string : strings)
        {
            std::cout << string << '\n';
        }
    }
};

int main(int argc, const char * argv[])
{

    func({"foo", "bar", "baz"});
    Func({"foo", "bar", "baz"});
    //auto ptr = std::make_shared<Func>({"foo", "bar", "baz"}); // won't compile.
    //auto ptr = std::make_shared<Func>{"foo", "bar", "baz"}; // nor this.
    return 0;
} …
Run Code Online (Sandbox Code Playgroud)

c++ clang shared-ptr initializer-list c++11

17
推荐指数
2
解决办法
2万
查看次数

标签 统计

c++ ×1

c++11 ×1

clang ×1

initializer-list ×1

shared-ptr ×1