c++ - std::initializer_list not able to be deduced from <brace-enclosed initializer list> -
i have class constructor takes initializer_list
:
foo::foo(std::initializer_list<bar*> bars)
if attempt create object brace-enclosed initializer list directly, initializer_list
correctly deduced:
foo f ({ &b }); // std::initializer_list<bar*> correctly deduced
however, when trying same indirectly (with variadic function template - in case make_unique
), compiler unable deduce initializer_list
:
std::make_unique<foo>({ &b }); // std::initializer_list<bar*> not deduced
error output:
error: no matching function call
‘make_unique(<brace-enclosed initializer list>)’
questions:
- why compiler failing deduce
{ &b }
initializer_list<boo*>
? - is possible use syntax
std::make_unique<foo>({ &b })
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 deduced { &b } foo f ({ &b }); // initializer_list not able deduced { &b } std::unique_ptr<foo> p = std::make_unique<foo>({ &b }); (void)f; return 0; }
make_unique
uses perfect forwarding.
perfect forwarding imperfect in following ways:
it fails forward initializer lists
it converts
null
or0
integer, can not passed value of pointer type.it not know type arguments be, cannot operations require knowing type. example:
struct foo { int x; }; void some_funcion( foo, foo ) {}; template<class...args> decltype(auto) forwarding( args&& ... args ) { return some_function(std::forward<args>(args)...); }
calling
some_function( {1}, {2} )
legal. constructsfoo
s{1}
,{2}
.calling
forwarding( {1}, {2} )
not. not know @ time callforwarding
argumentsfoo
s, cannot construct it, , cannot pass construction-initializer-list through code (as construction-lists not variables or expressions).if pass overloaded function name, overload cannot worked out @ point of call. , set of overloads not value, cannot perfect forward through.
you cannot pass bitfields through.
it forces takes reference arguments, if forwarded target not. "uses" static const data in ways can cause program technically ill-formed.
a reference array of unknown size
t(&)[]
cannot forwarded. can call function takingt*
it, however.
about half of these taken this comp.std.c++ thread, looked once remembered there other issues couldn't recall off top of head.
Comments
Post a Comment