Calling initializer_list constructor via make_unique/make_shared

后端 未结 2 1643
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 09:16

I\'m trying to use std::make_unique to instanciate a class whose constructor is to receive an std::initializer_list. Here a minimal case :

相关标签:
2条回答
  • 2020-11-29 09:29

    If you're prepared to type a few extra characters, you can do this:

    auto ptr = std::make_unique<Foo>( make_init_list( { "Hello"s , "World"s } ));
    

    where init_list is defined as

    template<typename T>
    std:: initializer_list<T> make_init_list ( std:: initializer_list<T> && l ) {
        return l;
    }
    

    This allows the deduction to occur, and is convenient if there are many places in the code you must do this.

    (Works on clang 3.9 and gcc 6.2.0. I got it to work on g++-4.8.4 also, except I had to tweak the std::string-literal and change to make_shared. But the deduction of T within make_init_list worked fine.)

    Actually, it this an extension? Is successful deduction for make_init_list required by the standard or not?

    0 讨论(0)
  • 2020-11-29 09:52

    std::make_unique is a function template which deduces the argument types which are passed to the object constructor. Unfortunately, braced lists are not deducible (with an exception for auto declarations), and so you cannot instantiate the function template when that missing parameter type.

    You can either not use std::make_unique, but please don't go that route – you should avoid naked news as much as you can, for the children's sake. Or you can make the type deduction work by specifying the type:

    • std::make_unique<Foo>(std::initializer_list<std::string>({"Hello", "World"}))

    • std::make_unique<Foo, std::initializer_list<std::string>>({"Hello", "World"})

    • auto il = { "Hello"s, "World"s }; auto ptr = std::make_unique<Foo>(il);

    The last option uses the special rule for auto declarations, which (as I hinted above) do in fact deduce an std::initializer_list.

    0 讨论(0)
提交回复
热议问题