std::initializer_list not able to be deduced from

前端 未结 2 2014
我寻月下人不归
我寻月下人不归 2021-01-20 01:05

I have a class whose constructor takes an initializer_list:

Foo::Foo(std::initializer_list bars)

If I attempt to c

2条回答
  •  悲&欢浪女
    2021-01-20 01:39

    A braced initializer has no type. When you call make_unique it tries to deduce the type and fails. In this case you have to specify the type when calling like

    std::make_unique(std::initializer_list{ &b });
    

    This will create a std::initializer_list which the compiler can deduce and it will forward it to Foo::Foo(std::initializer_list bars)

    The reason Foo f ({ &b }); works is that the compiler knows of the constructor Foo(std::initializer_list bars) and braced initializer list of B*s can be implicitly converted to a std::initializer_list. There is not type deduction going on.

提交回复
热议问题