可以默认类模板中的常规复制构造函数吗?

发布于 2025-01-31 04:56:29 字数 743 浏览 3 评论 0原文

在以下程序结构模板a具有consta< int>的构造函数中&。对于a< int>应该是复制构建器,可以默认:

template<typename T>
struct A {
    A() {}
    A(const A<int> &) = default;
};

int main() {
    A<int> a;
}

该程序被Clang接受,但GCC和MSVC都以错误:

 error C2610: 'A<T>::A(const A<int> &)': is not a special member function or comparison operator which can be defaulted
note: see reference to class template instantiation 'A<T>' being compiled

emo demo拒绝: https://gcc.godbolt.org/z/mn6nmbe1x

哪个编译器在这里正确?

In the following program struct template A has a constructor from const A<int> &. For A<int> it should be copy-constructor, which can be defaulted:

template<typename T>
struct A {
    A() {}
    A(const A<int> &) = default;
};

int main() {
    A<int> a;
}

This program is accepted by Clang, but both GCC and MSVC reject it with the error:

 error C2610: 'A<T>::A(const A<int> &)': is not a special member function or comparison operator which can be defaulted
note: see reference to class template instantiation 'A<T>' being compiled

Demo: https://gcc.godbolt.org/z/Mn6nMbe1x

Which compiler is right here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文