Il costruttore std :: shared_ptr non si comporta come mi aspettavo:std :: shared_ptr e inizializzazione liste
#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;
}
sto facendo qualcosa di sbagliato o è il compilatore? Il compilatore è:
$ clang ++ --version di Apple clang versione 4.0 (tag/Apple/clang-421.0.57) (sulla base di LLVM 3.1svn)
edit: shared_ptr invece di make_shared.
Ecco l'errore:
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.
Ultimo ho sentito, è noto che l'inoltro perfetto non è in realtà perfetto quando si tratta di elenchi di inizializzatori. – Puppy
{"foo", "bar", "baz"} non è un'espressione e come tale non ha tipo (tranne se usato con auto) .. Anche se sarebbe bello – Cubbi