Can a template function be called with missing template parameters in C++ ?

前端 未结 2 475
醉酒成梦
醉酒成梦 2021-01-24 12:47

This is an interview question, which has been done.

Which line has error ?

  #include
  template void foo(T op1, T op2)
         


        
2条回答
  •  旧巷少年郎
    2021-01-24 13:27

    When we use a function template, the compiler infers what template argument(s) to bind to the template parameter(s). Once the compiler determines the actual template argument(s), it instantiates an instance of the function for us. Essentially the compiler figures out what type to use in place of each type parameter. So, if op1 and op2 have the same type the template parameter(s) can be omitted (that's why line #2 causes error).

    From C++ primer

提交回复
热议问题