AFAIK在C++ 0x中被更改。
我想这只是一个疏忽(考虑到你总是可以通过放置函数作为类的一个static
成员来获得部分专业化效果和更详细的代码)。
如果有相关DR(缺陷报告),您可以查看相关DR(缺陷报告)。
编辑:检查这一点,我发现其他人也相信,但没有人能够在标准草案中找到任何此类支持。 This SO thread似乎表示函数模板的部分特化在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
}
对于'模板空隙F(T T,U U){}'也'模板<> void f(int t,char u){}'是允许的。 –
dashesy
2016-07-22 21:46:44
我觉得有趣的是,当问题不是“我怎么能达到类似的目标”,而是“这种行为背后的原因是什么”时,人们总是提供解决方法......我自己不知道这个选择的原因,但我认为委员会必须有理由禁止功能模板部分专业化。到目前为止,“最接近”的解释是Georgy发布的链接,该链接仅指出存在过载时功能模板部分专业化的潜在“风险”。但是,我不认为这是禁止此功能的理由,所以我认为还有更多这样的内容。 – bartgol 2017-10-03 19:58:39