Specialized template function with deleted “general” case fails to compile with g++

I don’t know if the following will be enlightening but I found defect report 941: Explicit specialization of deleted function template with status C++11 that states the following (Emphasis Mine): According to 14.7.3 [temp.expl.spec] paragraph 1, only non-deleted function templates may be explicitly specialized. There doesn’t appear to be a compelling need for this restriction, … Read more

Understanding (simple?) C++ Partial Template Specialization

Partial specialization of a function template, whether it is member function template or stand-alone function template, is not allowed by the Standard: template<typename T, typename U> void f() {} //okay – primary template template<typename T> void f<T,int>() {} //error – partial specialization template<> void f<unsigned char,int>() {} //okay – full specialization But you can partially … Read more

Can a single member of a class template be partially specialized?

The notion of partial specialization only exists for class templates (described by §14.5.5) and member templates (i.e. members of a template class that are themselves template functions, described by §14.5.5.3/2). It does not exist for ordinary members of class templates, nor does it exist for function templates – simply because it is not described by … Read more

Multiple SFINAE class template specialisations using void_t

There is a rule that partial specializations have to be more specialized than the primary template – both of your specializations follow that rule. But there isn’t a rule that states that partial specializations can never be ambiguous. It’s more that – if instantiation leads to ambiguous specialization, the program is ill-formed. But that ambiguous … Read more

How to know if a type is a specialization of std::vector?

In C++11 you can also do it in a more generic way: #include <type_traits> #include <iostream> #include <vector> #include <list> template<typename Test, template<typename…> class Ref> struct is_specialization : std::false_type {}; template<template<typename…> class Ref, typename… Args> struct is_specialization<Ref<Args…>, Ref>: std::true_type {}; int main() { typedef std::vector<int> vec; typedef int not_vec; std::cout << is_specialization<vec, std::vector>::value << is_specialization<not_vec, … 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

Will specialization of function templates in std for program-defined types no longer be allowed in C++20?

As it stands now it definitly looks that way. Previously [namespace.std] contained A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited. While the … 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