I have a class whose constructor takes an initializer_list
:
Foo::Foo(std::initializer_list<Bar*> bars)
If I attempt to create an object with a brace-enclosed initializer list directly, the initializer_list
is correctly deduced:
Foo f ({ &b }); // std::initializer_list<Bar*> correctly deduced
However, when trying to do the same indirectly (with a variadic function template - in this case make_unique
), the compiler is unable to deduce the initializer_list
:
std::make_unique<Foo>({ &b }); // std::initializer_list<Bar*> not deduced
Error output:
error: no matching function for call to
‘make_unique(<brace-enclosed initializer list>)’
Questions:
- Why is the compiler failing to deduce
{ &b }
as ainitializer_list<Boo*>
? - Is it possible to use the syntax
std::make_unique<Foo>({ &b })
which I desire?
Full example below:
#include <initializer_list>
#include <memory>
struct Bar
{};
struct Foo
{
Foo(std::initializer_list<Bar*> bars)
{ }
};
int main()
{
Bar b;
// initializer_list able to be deduced from { &b }
Foo f ({ &b });
// initializer_list not able to be deduced from { &b }
std::unique_ptr<Foo> p = std::make_unique<Foo>({ &b });
(void)f;
return 0;
}