继承 MSVC 2017 中的模板构造函数和错误 C2600

Inheriting template constructor and error C2600 in MSVC 2017

本文关键字:构造函数 错误 C2600 MSVC 2017 继承      更新时间:2023-10-16

我在VS 2017 15.9.4(工具集v141)上遇到了编译错误,它曾经与VS 2015(工具集v140)一起使用。问题在于从基类继承模板化构造函数。

#include <type_traits>
template <typename T>
class IAttribute {
public:
    template <
        typename U = T,
        typename = typename std::enable_if<std::is_default_constructible<U>::value>::type
    >
    IAttribute() {}
    IAttribute(T* value) {
    }
private:
    T* m_value;
};
class AttributeInt : public IAttribute<int> {
public:
    using IAttribute<int>::IAttribute;
    AttributeInt();
};
AttributeInt::AttributeInt() : IAttribute<int>(nullptr) {
}
int main() {
    AttributeInt qq;
}

在最新的VS中,我收到错误:

错误 C2600:"AttributeInt::AttributeInt":无法定义编译器生成的特殊成员函数(必须先在类中声明)

一段时间后,我发现将构造函数实现AttributeInt()移动到类定义主体可以修复错误。

class AttributeInt : public IAttribute<int> {
public:
    using IAttribute<int>::IAttribute;
    AttributeInt() : IAttribute<int>(nullptr) {}
};

然而,这并不能解决我的问题,因为在我的项目中,我需要构造依赖于这个类的派生类。任何想法如何在不移动实现的情况下解决此问题?

无论如何,原始代码适用于GCC 8.2,clang 7.0.0,zapc++ 2017.08和MSVC 2015。

> 它在VS 2017中被确认为错误,并在VS 2019 16.0.0预览版1中修复 - 在此处确认。无论如何,感谢您的帮助!