3 回答

TA貢獻2003條經驗 獲得超2個贊
我想這只是一個疏忽(考慮到通過將函數作為static類的成員,您總是可以使用更詳細的代碼來獲得部分專業化效果)。
您可能會查找相關的DR(缺陷報告)(如果有)。
編輯:檢查這一點,我發現其他人也相信這一點,但是沒有人能夠在標準草案中找到任何這樣的支持。該SO線程似乎表明C ++ 0x不支持功能模板的部分專業化。
編輯2:只是我的意思是“將函數作為static類的成員放置”的一個示例:
#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
}

TA貢獻1934條經驗 獲得超2個贊
好吧,您確實不能執行部分函數/方法專門化,但是可以執行重載。
template <typename T, typename U>
T fun(U pObj){...}
// acts like partial specialization <T, int> AFAIK
// (based on Modern C++ Design by Alexandrescu)
template <typename T>
T fun(int pObj){...}
是這樣,但我不知道它是否滿足您。
- 3 回答
- 0 關注
- 521 瀏覽
添加回答
舉報