c++ - 为什么不能在非完全专业化的类模板中定义完全专业化的类模板?
问题描述
为什么不能在非完全专业化的类模板中定义完全专业化的类模板?
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 (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
海合会错误:
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> {
^
叮当版本:
clang version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final)
Target: x86_64-pc-linux-gnu
叮当错误:
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() {
}
解决方案
推荐阅读
- php - 如何修复 Laradock 上的 SQLSTATE[HY000] [2002] Connection denied (SQL: select * from `categories` where `enable` = 1)?
- file-upload - 是否可以将环境变量“GOOGLE_APPLICATION_CREDENTIALS”设置为 Flowground 中上传的 JWT 文件?
- list - 如何使用 python2 在列表中提取 2 或 3 位小数?
- html - 如何保持数据列表在下拉菜单中显示项目?
- codeigniter - Codeigniter count_all_results() 和 result() with group_by, join, where vs
- c# - c# DataGridView DataGridViewTextBoxCell中的FindTextLocation或文本索引
- azure - 使用应用程序网关的 azure api 管理转换 URL 标头
- angular - 如何处理 Angular 4 打字稿中的无效十进制值?
- python - Unravel Index numpy - 自己的实现
- python - 如何在python中删除具有非数字数据的行