Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax of function Explicit specialization

Tags:

c++

Based on the tutorial ,

// Example 2: Explicit specialization 
// 
template<class T> // (a) a base template 
void f( T ){;}

template<class T> // (b) a second base template, overloads (a) 
void f( T* ){;}     //     (function templates can't be partially 
//     specialized; they overload instead)

template<>        // (c) explicit specialization of (b) 
void f<>(int*){;} // ===> Method one

I also test the following with VS2010 SP1 without any warning.

template<>        // (c) alternative
void f<int>(int*){;} // ==> Method two

Question> Which way is recommended way based on the C++ standard? Method one or Method two?

As you can see the key different between Method one and Method two is as follows:

template<>        
void f<>(int*){;}    // ===> Method one

template<>        
void f<int>(int*){;} // ===> Method two
       ^^^

Based on the tutorial, we should write the following plain old function instead:

void f(int*){;}

But that is NOT the question I am asking:)

Thank you

like image 829
q0987 Avatar asked Feb 12 '26 03:02

q0987


1 Answers

The full specialization declaration can omit explicit template arguments when template being specialized can be determined via argument deduction (using as argument types the parameter types provided in the declaration) and partial ordering.[from "C++ Templates" by Vandervoode, Josuttis]

This it the case in your example so you can write:

template<>
void f(int){;}

to specialize (a) and

template<>
void f(int*){;}

to specialize (b).

like image 174
Bojan Komazec Avatar answered Feb 13 '26 18:02

Bojan Komazec