函数语法显式特化

时间:2012-04-14 18:39:43

标签: c++

基于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

我还在没有任何警告的情况下使用VS2010 SP1测试以下内容。

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

问题&GT;基于C ++标准的推荐方式是哪种方式?方法一还是方法二?

正如您所看到的,方法一和方法二之间的关键点如下:

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

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

根据教程,我们应该编写以下普通旧函数:

void f(int*){;}

但这不是我要问的问题:)

谢谢

1 个答案:

答案 0 :(得分:1)

  

完整的专业化声明可以省略显式模板   模板专用的参数可以通过确定   参数推导(使用参数类型作为参数类型)   在声明中提供)和部分排序。[来自“C ++   模板“由Vandervoode,Josuttis]

这是你的例子中的情况所以你可以写:

template<>
void f(int){;}

专门化(a)和

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

专门化(b)。

相关问题