显式模板特化的语法

Syntax for explicit template specializations

当编译为 C++98 或 C++11 时,gcc-4.9.2 和 clang-3.8 都接受以下内容,

#include <cstdio>

template <typename T> void f(T) { printf("T\n"); }
template <> void f<int>(int) { printf("int\n"); }    // explicit specialization
template <> void f<>(double) { printf("double\n"); } // explicit specialization -- 14.7.2(7)
template <> void f(float) { printf("float\n"); }     // HERE

int main() {
  f(1L);    // T
  f(10);    // int
  f(10.0);  // double
  f(10.0F); // float
}

我看到在 C++11 标准 §14.7.2(7) 中,允许在显式模板特化中推导尾随模板参数,但我找不到标记为 HERE 的简洁形式是否或如何使用允许。

这些编译器是否符合标准或者这是一些扩展?

C++14 标准 §14.7(3)

An explicit specialization may be declared for a function template, a class template, a member of a class template or a member template. An explicit specialization declaration is introduced by template<>. In an explicit specialization declaration for a class template, a member of a class template or a class member template, the name of the class that is explicitly specialized shall be a simple-template-id. In the explicit specialization declaration for a function template or a member function template, the name of the function or member function explicitly specialized may be a template-id.

然后演示

template<class U> void g(U) { }
template<> void g(char) { }       //specialize for U == char
                                  // U is deduced from the parameter type

然后我们有 §14.7.3(10)

A trailing template-argument can be left unspecified in the template-id naming an explicit function template specialization provided it can be deduced from the function argument type. [ Example:

template<class T> class Array { / ... / };
template<class T> void sort(Array<T>& v);

// explicit specialization for sort(Array<int>&)
// with deduced template-argument of type int
template<> void sort(Array<int>&);

—end example ]