模板VS 2005版中的模板
template inside template VS 2005 issue?
下面的代码(它用clang和gcc编译得很好)。问题是这个代码违反了C++03标准,或者这是VS 2005的错误?如果这是错误,有什么解决办法吗?
更新:我通过使用前向声明找到了解决方法:
//forward declaration
template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
struct FilteredConstructor;
//implementation
template<typename T>
class FilteredConstructor<T, true> {/*code here*/};
但关于符合标准的此类代码是否有效的问题仍然存在
namespace {
struct CoreTypesFilter {
template<typename T> struct Acceptor {
static const bool IsAccepted = false;
};
};
}
template<class Filter>
class QVariantConstructor {
template<typename T, bool IsAcceptedType = Filter::template Acceptor<T>::IsAccepted>
struct FilteredConstructor {
FilteredConstructor(const QVariantConstructor &tc) {}
};
template<typename T>
struct FilteredConstructor<T, /* IsAcceptedType = */ false> {
FilteredConstructor(const QVariantConstructor &tc) {}
};
public:
template<typename T>
void delegate(const T*)
{
FilteredConstructor<T> tmp(*this);
}
};
//comment or uncomment them to build on VS or linux
#define _TCHAR char
#define _tmain main
int _tmain(int argc, _TCHAR* argv[])
{
QVariantConstructor<CoreTypesFilter> vc;
vc.delegate("test");//this line trigger compile error
return 0;
}
VS 2005编译器的编译错误:
错误C2976:"QVariantConstructor::FilteredConstructor":模板参数太少1> 与1>[1> Filter="匿名命名空间"::CoreTypesFilter1> ]1> 请参阅"QVariantConstructor::FilteredConstructor"的声明1> 与1>[1> Filter="匿名命名空间"::CoreTypesFilter1> ]1> 请参阅对正在编译的函数模板实例化"void QVariantConstructor::delegate(const T*)"的引用1> 与1>[1> Filter="匿名命名空间"::CoreTypesFilter,1> T=字符1> ]1> 错误C2514:"QVariantConstructor::FilteredConstructor":类没有构造函数1> 与1>[1> Filter="匿名命名空间"::CoreTypesFilter1> ]1> 请参阅"QVariantConstructor::FilteredConstructor"的声明1> 与1>[1> Filter="匿名命名空间"::CoreTypesFilter1> ]
我快速阅读了该标准的相关章节,问题似乎归结为您如何阅读ISO/IEC 14882:2003第14.1节第9段
默认模板参数是在template-parameter
中的=
之后指定的template-argument
(14.3)。可以为任何类型的template-parameter
(类型、非类型、模板)指定默认template-argument
。默认template-argument
可以在类模板声明或类模板定义中指定。不应在函数模板声明或函数模板定义中指定默认template-argument
,也不应在类模板成员定义的template-parameter-list
中指定。不应在友元模板声明中指定默认template-argument
。
严格来说,这似乎意味着你的代码是非法的。然而,大多数实现似乎将其解释为已经声明的模板成员的定义。
健康警告,这是基于第一次阅读。我的答案很可能是错误的或不完整的,请纠正我。
相关文章:
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 数据成员SFINAE的C++17测试:gcc vs clang
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 正在VS调试器中监视映射条目
- Confusion: decltype vs std::function
- 将IBM Rhapsody模型集成到VS 2019中
- VS 2017 社区收到链接器错误,但专业版没有
- 从 MFC C++ 6.0 应用程序迁移到 VS 2005
- VS 2017 C++编译器在 VS 2005 中找不到匹配函数
- 无法创建或打开 *.VS 2015 中的 VC.db 文件(桌面快速版)更新 3RC
- 旧版 MFC 代码 (2005) 无法在 VC 2010 上编译
- 将C++代码从VS 2005转换为VS 2008?这甚至意味着什么
- 将用VS 2005编写的C++网络代码迁移到VS2010中的Visual C++
- 桌面版VS的Resharper C++
- 为什么使用VS 2012构建的MFC对话框的可执行文件大小几乎是使用VS 2005构建的对话框的3倍
- 模板VS 2005版中的模板
- VS 2005-命令行程序崩溃
- 错误 C1083:无法打开包含文件:"stdafx.h":VS 2005 中没有此类文件或目录