emplace_back not working with std::vector>

后端 未结 3 1928
我寻月下人不归
我寻月下人不归 2021-01-17 08:00

I am trying to do emplace_back into a std::vector>, but could not find the right syntax to do it.

#i         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-17 08:16

    One can achieve that using a helper function as follows:

     #include 
     #include 
    
     void emplace_work_around(
        std::vector>& v,
        std::initializer_list> && item
     )
     {
        v.emplace_back(std::forward>>(item));
     }
    
    int main()
    {
        std::vector> v;
    
        emplace_work_around(v,{{1,2}});
    }
    

    The problem was when we write:

    v.emplace_back({{1,2}});  // here {{1,2}} does not have a type.
    

    the compiler is not able to deduce the type of the argument, and it can't decide which constructor to call.

    The underlying idea is that when you write a function like

    template
    void f(T) {}
    

    and use it like

    f( {1,2,3,4} ); //error
    

    you will get compiler error, as {1,2,3,4} does have a type.

    But if you define your function as

    template
    void f(std::initializer_list) {}
     f( {1,2,3,4} );
    

    then it compiles perfectly.

提交回复
热议问题