Having trouble passing multiple initializer lists to variadic function template

后端 未结 4 2156
自闭症患者
自闭症患者 2020-12-20 14:16

I don\'t understand the error message when trying to pass a variable number of initializer lists:

template
void foo(Values...)
{
}
         


        
4条回答
  •  爱一瞬间的悲伤
    2020-12-20 15:00

    The problem is indeed deducibility, as mentioned by other answers. Instead of providing a second function taking an initializer_list, you may specify the type of the argument to foo when calling the function:

    #include 
    
    template
    void foo(Values...)
    {
    }
    
    int main()
    {
      foo(1, 2, 3, "hello", 'a');
      foo(std::initializer_list{1}, std::initializer_list{2, 3});
    }
    

    Deciding how to treat each parameter, is another question, however.

    [EDIT]: Idea is taken from std::shared_ptr and initializer lists

提交回复
热议问题