Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why function template cannot be partially specialized?

I know the language specification forbids partial specialization of function template.

I would like to know the rationale why it forbids it? Are they not useful?

template<typename T, typename U> void f() {}   //allowed! template<> void f<int, char>()            {}   //allowed! template<typename T> void f<char, T>()    {}   //not allowed! template<typename T> void f<T, int>()     {}   //not allowed! 
like image 298
Nawaz Avatar asked Feb 24 '11 07:02

Nawaz


People also ask

What is function template specialization?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.

What is the specialty of a template function give example?

Template in C++is a feature. We write code once and use it for any data type including user defined data types. For example, sort() can be written and used to sort any data type items. A class stack can be created that can be used as a stack of any data type.

Can a function template be overloaded?

You may overload a function template either by a non-template function or by another function template. The function call f(1, 2) could match the argument types of both the template function and the non-template function.

What is the importance of function templates?

Function templates. Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters.


1 Answers

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 that others have also believed that, but no-one is able to find any such support in the draft standard. This SO thread seems to indicate that partial specialization of function templates is not supported in C++0x.

EDIT 2: just an example of what I meant by "placing the function as a static member of a class":

#include <iostream> using namespace std;  // template<typename T, typename U> void f() {}   //allowed! // template<> void f<int, char>()            {}   //allowed! // template<typename T> void f<char, T>()    {}   //not allowed! // template<typename T> void f<T, int>()     {}   //not allowed!  void say( char const s[] ) { std::cout << s << std::endl; }  namespace detail {     template< class T, class U >     struct F {         static void impl() { say( "1. primary template" ); }     };      template<>     struct F<int, char> {         static void impl() { say( "2. <int, char> explicit specialization" ); }     };      template< class T >     struct F< char, T > {         static void impl() { say( "3. <char, T> partial specialization" ); }     };      template< class T >     struct F< T, int > {         static void impl() { say( "4. <T, int> partial specialization" ); }     }; }  // namespace detail  template< class T, class U > void f() { detail::F<T, U>::impl(); }      int main() {     f<char const*, double>();       // 1     f<int, char>();                 // 2     f<char, double>();              // 3     f<double, int>();               // 4 } 
like image 178
Cheers and hth. - Alf Avatar answered Sep 27 '22 22:09

Cheers and hth. - Alf