C++:嵌套模板类错误"explicit specialization in non-namespace scope"
C++: Nested template classes error "explicit specialization in non-namespace scope"
以下代码:
template <class T1>
struct A1
{
template <int INDEX>
struct A2 { /* ... */ };
template <>
struct A2<-1> { /* ... */ };
};
int main()
{
A1<int>::A2<-1> x;
}
出现以下错误:
prog.cpp:7:13:错误:非命名空间作用域
'struct A1<T1>'
中的显式专用化prog.cpp:8:10:错误:部分专用化中未使用模板参数:
prog.cpp:8:10:错误:'T1'
如何最好地解决此错误?我试过这个:
template <class T1>
struct A1
{
template <int INDEX, class DUMMY = void>
struct A2 { /* ... */ };
template <class DUMMY>
struct A2<-1, DUMMY> { /* ... */ };
};
int main()
{
A1<int>::A2<-1> x;
}
这似乎奏效了,但也有点像一个软糖。
有没有更好的方法来解决这个问题?
我查阅了以前的答案,只能在类中找到具有函数的答案,而不能在类中查找。我在其他答案中也发现了"假人"的把戏,但我想知道是否有更好的解决方案。
另外,作为旁注,C++允许的第一个代码是0x吗?
不允许在不专门化A1
的情况下显式专门化A2
(§14.7.3/18)。C++0x具有相同的限制(n3242§14.6.3/16)。同时允许嵌套类的部分专门化。所以使用伪类的技巧是好的。
相关文章:
- 使用显式模板参数列表和 [temp.arg.explicit]/3 的函数调用的演绎失败
- 统一初始化是隐式发生的,即使 int 强制转换运算符是使用 explicit 关键字声明的.原因是什么?
- 书籍示例在类声明中使用"explicit"关键字。无效吗?
- 我如何使用explicit模板实例化用于类定义中定义的模板成员函数
- 外部模板'inconsistent explicit instantiations'
- "Explicit specialization of std::iterator_traits<char *> after instantiation"(咕)
- Visual Studio 2013 'explicit'关键字错误?
- "explicit"构造函数对过载解决的影响
- 为什么 'explicit' 关键字允许隐式转化?
- 提升::shared_ptr<> "explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete"
- glBufferData with explicit array vs non explicit array
- 'explicit' g ++ 中的关键字对简单构造函数(不是复制/赋值构造函数)没有影响?
- 错误:"explicit specialization requires 'template <>'"
- 在std::auto_ptr的构造函数中使用' explicit '关键字有什么原因吗?
- 在对类方法执行指针部分专用化时获取"illegal use of explicit template arguments"
- 这个使用 C++ 'explicit' 关键字的示例是否正确?
- 继承的构造函数和"explicit is better than implicit"
- 无法识别子进程中的Googletest Explicit FAIL()
- "explicit"关键字对默认构造函数有任何影响吗?
- “explicit”对静态上播的影响