#include <iostream>
#include <memory>
class Base
{
public:
Base() {}
};
class Derived : public Base
{
public:
Derived() {}
Derived(std::initializer_list<std::pair<int, std::shared_ptr<Base>>>) {}
};
int main(int argc, char ** argv)
{
auto example = new Derived({
{ 0, std::make_shared<Derived>() }
});
return 0;
}
它正常工作 (现场预览),但当我尝试使用std::initializer_list
作为参数与std::make_shared
一起使用时,我收到错误:
auto example = new Derived({
{ 0, std::make_shared<Derived>({
{ 0, std::make_shared<Derived>() }
}) }
});
正如您在实时预览中所看到的。
错误:函数的参数太多...
只有这样做它才有效(实时预览):
auto example = new Derived({
{ 0, std::make_shared<Derived>(std::initializer_list<std::pair<int, std::shared_ptr<Base>>> {
{ 0, std::make_shared<Derived>() }
}) }
});
我想知道的是: 为什么只有当我将 std::initializer_list
作为参数传递给 std::make_shared
,而不是像这样使用 {{}}
时才能起作用。auto example = new Derived({ { 0, std::make_shared<Base>() } });
能否让std::make_shared
接受它?
先行致谢。
make_shared
的模板类型推断无法推断它的类型。 - dyp