Konstruktor std :: shared_ptr nie zachowuje się zgodnie z oczekiwaniami:std :: shared_ptr i initializer listy
#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;
}
Czy robię coś źle lub jest kompilator? Kompilator jest:
$ dzyń ++ --version Jabłko dzyń wersja 4.0 (tags/Apple/dzyń-421.0.57) (oparty na LLVM 3.1svn)
edit: shared_ptr zamiast make_shared.
Oto błąd:
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.
Ostatnio słyszałem, wiadomo, że perfekcyjne przekazywanie nie jest w ogóle doskonałe, jeśli chodzi o listy inicjalizatorów. – Puppy
{"foo", "bar", "baz"} nie jest wyrażeniem i jako takie nie ma typu (z wyjątkiem użycia z auto). Chociaż byłoby dobrze – Cubbi