See Should I use () or {} when passing arguments? . foo is a clone of std::vector .
In N4140 unique.ptr.create std::make_unique specified like this:
template <class T, class... Args> unique_ptr<T> make_unique(Args&&... args);
This means that to initialize objects for implementation, you must use () instead of {} . As an example, the following
auto s1 = std::make_unique<foo>(3, 1).get()->size(); auto s2 = std::make_unique<foo>(1).get()->size(); auto s3 = std::make_unique<foo>(2).get()->size(); std::cout << s1 << s2 << s3;
outputs 312 , whereas when using {} (inside std::make_unique ) 211 will be output. Since initializer lists cannot be displayed, std::initializer_list must be explicitly passed in order to get the last result. The question is, why is not the overload as provided?
namespace test { template <class T, class Deduce> std::unique_ptr<T> make_unique(std::initializer_list<Deduce> li) { return ::std::make_unique<T>(li); } }; int main() { auto p1 = test::make_unique<foo>({3, 1}).get()->size(); auto p2 = test::make_unique<foo>({1}).get()->size(); auto p3 = test::make_unique<foo>({2}).get()->size(); std::cout << p1 << p2 << p3; }
Conclusion 211 .
I do not consider the reasons “you can write yourself” or “to avoid inflating the standard”, to be very good reasons. Are there any flaws in providing this overload?
source share