为什么不能在非完全专业的类模板中定义完全专业的类模板?
template<typename TW>
struct Wrapper {
template<typename T>
struct Fun_ {
constexpr static int vlaue = 0;
};
template<> // error
struct Fun_<int> { // error
constexpr static int value = 1;
};
};
int main() {
}
GCC版本:
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
GCC错误:
test.cpp:7:14: error: explicit specialization in non-namespace scope ‘struct Wrapper<TW>’
template<>
^
test.cpp:8:12: error: template parameters not deducible in partial specialization:
struct Fun_<int> {
^
C语版本:
clang version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final)
Target: x86_64-pc-linux-gnu
C语错误:
test.cpp:8:12: error: explicit specialization of 'Fun_' in class
scope
struct Fun_<int> {
^
但是,通过使用附加的伪模板参数将完全专业化的类模板转换为部分专业化的模板,可以编译以下内容:
template<typename TW>
struct Wrapper {
// TDummy is a pseudo template argument with default value void
template<typename T, typename TDummy = void>
struct Fun_ {
constexpr static int value = 0;
};
template<typename TDummy>
struct Fun_<int, TDummy> { // partial specialization
constexpr static int value = 1;
};
};
int main() {
}