【问题标题】:Why can't Visual C++ partially specialize a class inside a template?为什么 Visual C++ 不能部分特化模板内的类?
【发布时间】:2018-10-04 05:07:11
【问题描述】:

当我尝试在 Visual C++ (2015) 中运行此代码时

template<int V>
struct outer
{
    template<int U, bool>
    struct inner;
};
template<int V>
template<bool B>
struct outer<V>::inner<V, B> { enum { value = 0 }; };

int main()
{
    return outer<1>::inner<1, false>::value;
}

我得到了错误

Temp.cpp(13): error C2027: use of undefined type 'outer<1>::inner<1,false>'
Temp.cpp(13): note: see declaration of 'outer<1>::inner<1,false>'
Temp.cpp(13): error C2065: 'value': undeclared identifier

但是,it compiles and runs fine on GCC 和 Clang。

三个问题:

  1. 如果部分特化不是部分特化,它在做什么

  2. 为什么会发生这种情况?是 bug 还是这段代码真的有问题?

  3. 是否有一种解决方法可以让您在内部模板类中仍然使用内部模板类,或者是将模板参数移到外部的唯一解决方案?

【问题讨论】:

  • 升级到 VS 2017?它在那里编译而没有抱怨。
  • @1201ProgramAlarm:Ack,所以这是一个错误?不幸的是,升级说起来容易做起来难......
  • 无法用 VS2017 重现。

标签: c++ templates visual-c++ inner-classes partial-specialization


【解决方案1】:

这是 Visual C++ 2015(众多之一)中 C++ 实现的一个已知限制。

这在 Visual C++ 2017 中确实有效,因此可能需要进行版本升级。

【讨论】:

  • 您能否解决帖子中的其他问题?
猜你喜欢
  • 2011-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-14
  • 1970-01-01
相关资源
最近更新 更多