emplace_back

六月 04, 2022

emplace_back

emplace_back({1,2})方式

https://stackoverflow.com/questions/20391632/how-to-use-stdvectoremplace-back-for-vectorvectorint

#include <initializer_list>
#include <type_traits>

template<typename T>
void func(T arg) {
}


int main() {
    auto init_list = {1, 2};    // This works because of a special rule
    static_assert(std::is_same<decltype(init_list), std::initializer_list<int>>::value, "not same");

    func(std::initializer_list<int>{1, 2});     // Ok. Has explicit type.

    func({1, 2});   // This doesn't because there's no rule for function
                    //      template argument to deduce std::initializer_list
                    //      in this form.
}