std :: shared_ptr和初始化列表 [英] std::shared_ptr and initializer lists

查看:1342
本文介绍了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<< '\\\
';
}
}

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

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

func({foo,bar,baz}) ;
Func({foo,bar,baz});
// auto ptr = std :: make_shared< Func>({foo,bar,baz}); //将不会编译。
// auto ptr = std :: make_shared< Func> {foo,bar,baz}; //也不是这样。
return 0;
}

我做错了什么或是编译器?编译器是:



$ clang ++ --version
Apple clang版本4.0(标签/ Apple / clang-421.0.57)(基于LLVM 3.1svn)



以下是错误:




$ b

编辑:shared_ptr而不是make_shared。 b $ b

  make -k 
clang ++ -std = c ++ 11 -stdlib = libc ++ main.cc -o main
main.cc:28: 18:错误:没有匹配的函数调用'make_shared'
auto ptr = std :: make_shared< Func>({foo,bar,baz});
^ ~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/c++/v1/memory:4621:1:注意:候选函数不可行:
需要0个参数,但提供了1个
make_shared(_Args& ...__ args)
^
1生成错误。


解决方案

尝试:

  auto ptr = std :: make_shared< Func>(std :: initializer_list< std :: string> {foo,bar,baz} ); Clang不愿意推导 {foo,b和b的类型。
。我目前不确定这是该语言的工作方式,或者如果我们在看一个编译器错误。


The std::shared_ptr constructor isn't behaving as I expected:

#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;
}

Am I doing something wrong or is the compiler? The compiler is:

$ clang++ --version Apple clang version 4.0 (tags/Apple/clang-421.0.57) (based on LLVM 3.1svn)

edit: shared_ptr instead of make_shared.

Here's the error:

make -k 
clang++ -std=c++11 -stdlib=libc++    main.cc   -o main
main.cc:28:18: error: no matching function for call to 'make_shared'
      auto ptr = std::make_shared<Func>({"foo", "bar", "baz"});
                 ^~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/c++/v1/memory:4621:1: note: candidate function not viable:
     requires 0 arguments, but 1 was provided
make_shared(_Args&& ...__args)
^
1 error generated.

解决方案

Try this:

auto ptr = std::make_shared<Func>(std::initializer_list<std::string>{"foo", "bar", "baz"});

Clang is not willing to deduce the type of {"foo", "bar", "baz"}. I'm currently not sure whether that is the way the language is supposed to work, or if we're looking at a compiler bug.

这篇关于std :: shared_ptr和初始化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆