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

Template partial specialization for integral non-type parameters and non-integral non-types, difference between g++ and clang

I will dedicate my answer to case #4, because according to the OP’s EDIT, the compilers now agree on cases #6-8: # | The code | g++ (6.1) | clang++ (3.8.0) | 4 | foo<double&, d, d> | #1 — why? | #2 as expected | Seems like clang++ 3.8.0 behaves correctly and gcc 6.1 … Read more

std::remove_reference explained?

why is it that there are specializations for lvalue and rvalue reference? If only the primary template existed, then doing: remove_reference<int&>::type Would give you: int& And doing: remove_reference<int&&>::type Would give you: int&& Which is not what you want. The specializations for lvalue references and rvalue references allow stripping the & and the &&, respectively, from … Read more

C++ templates specialization syntax

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

Template Specialization VS Function Overloading

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 function template cannot 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

C++ function template partial specialization?

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)