What is the correct syntax for defining a specialization of a function template?

Here are comments with each syntax: void foo(int param); //not a specialization, it is an overload void foo<int>(int param); //ill-formed //this form always works template <> void foo<int>(int param); //explicit specialization //same as above, but works only if template argument deduction is possible! template <> void foo(int param); //explicit specialization //same as above, but works … Read more

Should you prefer overloading over specialization of function templates?

Short story: overload when you can, specialise when you need to. Long story: C++ treats specialisation and overloads very differently. This is best explained with an example. template <typename T> void foo(T); template <typename T> void foo(T*); // overload of foo(T) template <> void foo<int>(int*); // specialisation of foo(T*) foo(new int); // calls foo<int>(int*); Now … Read more

Why can function templates not be partially specialized?

AFAIK that’s changed in C++0x. I guess it was just an oversight (considering that you can always get the partial specialization effect with more verbose code, by placing the function as a static member of a class). You might look up the relevant DR (Defect Report), if there is one. EDIT: checking this, I find … Read more

Why can I seemingly define a partial specialization for function templates?

Function partial specialization is not allowed yet as per the standard. In the example, you are actually overloading & not specializing the max<T1,T2> function. Its syntax should have looked somewhat like below, had it been allowed: // Partial specialization is not allowed by the spec, though! template <typename T> inline T const& max<T,T> (T const& … Read more

Is std::vector a `user-defined type`?

Prof. Stroustrup is very clear that any type that is not built-in is user-defined. See the second paragraph of section 9.1 in Programming Principles and Practice Using C++. He even specifically calls out “standard library types” as an example of user-defined types. In other words, a user-defined type is any compound type. Source The article … Read more

C++ specialization of template function inside template class

AFAIK (and the standards experts can correct me), you cannot specialize a templated function of a class template without specializing the class itself… i.e. the following I think will work: template <> template <> int X<Y>::getAThing<int>(std::string param) { return getIntThing(param); // Some function that crunches on param and returns an int. }

Why are there so many specializations of std::swap?

So what it is gained from specializing std::pair? Performance. The generic swap is usually good enough (since C++11), but rarely optimal (for std::pair, and for most other data structures). I’m also left wondering if I should be writing my own specializations for custom classes, or simply relying on the template version. I suggest relying on … Read more

Partial specialization of variadic templates

14.8.2.4, section 11 (I refer to draft N3242). In most cases, all template parameters must have values in order for deduction to succeed, but for partial ordering purposes a template parameter may remain without a value provided it is not used in the types being used for partial ordering. [ Note: A template parameter used … Read more

Why is it not possible to overload class templates?

Section 12.5 from Templates the Complete Guide (Amazon) contains this quote: You may legitimately wonder why only class templates can be partially specialized. The reasons are mostly historical. It is probably possible to define the same mechanism for function templates (see Chapter 13). In some ways the effect of overloading function templates is similar, but … Read more