【问题标题】:Constructor inheritance for class derived from template class in visual studio 2015 rc从 Visual Studio 2015 rc 中的模板类派生的类的构造函数继承
【发布时间】:2015-07-05 16:36:19
【问题描述】:

根据 msvs2015rc 的page 应该支持新特性构造函数继承。是的,它适用于这样的简单情况:

struct B { 
    B(int) {}
};
struct D : B {
    using B::B; // now we can create D object with B's constructor 
};

但如果我尝试创建更复杂的示例:

template <class T>
struct B
{
    B(int) {} 
};

template <template <class> class C, class T>
struct D : C<T>
{
    using C<T>::C;
};

int main() {

    D<B,int> d(42);
    return 0;
}

我遇到了编译器错误:

  • error C2039: 'C': is not a member of 'B&lt;T&gt;'
  • error C2873: 'C': symbol cannot be used in a using-declaration
  • error C2664: 'D&lt;B,int&gt;::D(D&lt;B,int&gt; &amp;&amp;)': cannot convert argument 1 from 'int' to 'const D&lt;B,int&gt; &amp;'

当且仅当将模板模板类 C 重命名为 B 时,我才能消除这些错误:

template <template <class> class B, class T>
struct D : B<T>
{
    using B<T>::B;
};

我认为这是一个编译器错误,因为所有这些代码都使用 gcc/clang 编译得很好。

有人对这个问题有其他看法吗?

【问题讨论】:

  • 也许你在某处缺少templatetypename

标签: c++ templates inheritance constructor visual-studio-2015


【解决方案1】:

这是 MSVC 中的一个错误,它也出现在他们在线提供的最新版本中。所以,请提交错误报告。相关讨论可以在other SO question 中找到。它包含一些标准的摘录,解释了为什么它应该起作用。

【讨论】:

    猜你喜欢
    • 2015-11-03
    • 2012-12-19
    • 1970-01-01
    • 2019-12-14
    • 2021-04-20
    • 1970-01-01
    • 2016-11-29
    • 2016-03-04
    • 2016-05-23
    相关资源
    最近更新 更多