Syntax for explicit custom patterns

When compiling as C ++ 98 or C ++ 11, gcc-4.9.2 and clang-3.8 are accepted,

#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
}

I see that the C ++ 11 standard §14.7.2 (7) allows the output of template trailing arguments in explicit specialized specializations, but I cannot determine if the terser form marked is allowed HERE.

Are these compilers appropriate or is it some kind of extension?

+4
source share
1 answer

C ++ 14 Standard §14.7 (3) has

​​ , , . < > . , , , , simple-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)

- , , , . [:

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 ]

+3

Source: https://habr.com/ru/post/1609033/


All Articles