C++ - What is the purpose of function template specialization? When to use it?

前端 未结 6 1995
北恋
北恋 2021-01-13 11:31

Learning C++, came upon function templates. The chapter mentioned template specialization.

  1. template <> void foo(int);

6条回答
  •  情书的邮戳
    2021-01-13 11:53

    I find it very important. You can use this as you would use a virtual method. There would be no point in virtual methods unless some of them were specialized. I have used it a lot to differentiate between simple types (int,short,float) and objects, object pointers and object references. An example would be serialize/unserialize methods that would handle objects by calling the objects serialize/unserialize method, while simple types should be written directly to a stream.

提交回复
热议问题