QT6和类特异性操作员的编译问题新

发布于 2025-02-11 23:16:23 字数 2422 浏览 2 评论 0原文

考虑一个具有自定义操作员新的课程。

class BaseClass {
  void* operator new(size_t size);
  void operator delete(void* p);
};

class MyClass : public BaseClass {
public:
  MyClass(int);
};

然后使用myClass作为信号的参数和QT中的

qt5 slot,但使用QT6使用QT6在使用Visual Studio 2019时在qmetatype中生成深度的汇编错误,而无法找到特定于类的类别myClass的新位置。

vs 2019的汇编错误(更改为隐藏特定路径和类名称):

...QtCore\qmetatype.h(2313): error C2660: 'BaseClass::operator new': function does not take 3 arguments
: note: see declaration of 'BaseClass::operator new'
...QtCore\qmetatype.h(2310): note: while compiling class template member function 'QtPrivate::QMetaTypeInterface::CopyCtrFn QtPrivate::QMetaTypeForType<T>::getCopyCtr(void)'
        with
        [
            T=Ty
        ]
...QtCore\qmetatype.h(2370): note: see reference to class template instantiation 'QtPrivate::QMetaTypeForType<T>' being compiled
        with
        [
            T=Ty
        ]
...QtCore\qmetatype.h(2494): note: see reference to class template instantiation 'QtPrivate::QMetaTypeInterfaceWrapper<Ty>' being compiled
...QtCore\qmetatype.h(2537): note: see reference to function template instantiation 'const QtPrivate::QMetaTypeInterface *QtPrivate::qTryMetaTypeInterfaceForType<Unique,QtPrivate::TypeAndForceComplete<MyClass &,std::false_type>>(void)' being compiled
    with
    [
        Unique=qt_meta_stringdata_..._t
    ]
moc_xxx.cpp(286): note: see reference to variable template 'const QtPrivate::QMetaTypeInterface *const qt_incomplete_metaTypeArray<qt_meta_stringdata_ModelBrowser_t,QtPrivate::TypeAndForceComplete<ModelBrowser,std::integral_constant<bool,1> >,QtPrivate::TypeAndForceComplete<void,std::integral_constant<bool,0> >,QtPrivate::TypeAndForceComplete<void,std::integral_constant<bool,0> >,QtPrivate::TypeAndForceComplete<MyClass &,std::integral_constant<bool,0> >,QtPrivate::TypeAndForceComplete<void,std::integral_constant<bool,0> >,QtPrivate::TypeAndForceComplete<MyClass &,std::integral_constant<bool,0> >,QtPrivate::TypeAndForceComplete<void,std::integral_constant<bool,0> >,...[58]' being compiled

尝试添加通用类特定的位置新(也就是void* operator* operator new new(size_t size,std :: align_val_t,void*),void*))似乎可能是可能的,但根据标准和其他编译器中的失败而不是。

Consider a class with custom operator new.

class BaseClass {
  void* operator new(size_t size);
  void operator delete(void* p);
};

class MyClass : public BaseClass {
public:
  MyClass(int);
};

And then using MyClass as an argument for a signal and slot in Qt

With Qt5 that works, but with Qt6 that generates compilation errors deep in QMetaType when using Visual Studio 2019, by failing to find a class-specific placement new for MyClass.

Compilation error with VS 2019 (changed to hide specific paths and class-names):

...QtCore\qmetatype.h(2313): error C2660: 'BaseClass::operator new': function does not take 3 arguments
: note: see declaration of 'BaseClass::operator new'
...QtCore\qmetatype.h(2310): note: while compiling class template member function 'QtPrivate::QMetaTypeInterface::CopyCtrFn QtPrivate::QMetaTypeForType<T>::getCopyCtr(void)'
        with
        [
            T=Ty
        ]
...QtCore\qmetatype.h(2370): note: see reference to class template instantiation 'QtPrivate::QMetaTypeForType<T>' being compiled
        with
        [
            T=Ty
        ]
...QtCore\qmetatype.h(2494): note: see reference to class template instantiation 'QtPrivate::QMetaTypeInterfaceWrapper<Ty>' being compiled
...QtCore\qmetatype.h(2537): note: see reference to function template instantiation 'const QtPrivate::QMetaTypeInterface *QtPrivate::qTryMetaTypeInterfaceForType<Unique,QtPrivate::TypeAndForceComplete<MyClass &,std::false_type>>(void)' being compiled
    with
    [
        Unique=qt_meta_stringdata_..._t
    ]
moc_xxx.cpp(286): note: see reference to variable template 'const QtPrivate::QMetaTypeInterface *const qt_incomplete_metaTypeArray<qt_meta_stringdata_ModelBrowser_t,QtPrivate::TypeAndForceComplete<ModelBrowser,std::integral_constant<bool,1> >,QtPrivate::TypeAndForceComplete<void,std::integral_constant<bool,0> >,QtPrivate::TypeAndForceComplete<void,std::integral_constant<bool,0> >,QtPrivate::TypeAndForceComplete<MyClass &,std::integral_constant<bool,0> >,QtPrivate::TypeAndForceComplete<void,std::integral_constant<bool,0> >,QtPrivate::TypeAndForceComplete<MyClass &,std::integral_constant<bool,0> >,QtPrivate::TypeAndForceComplete<void,std::integral_constant<bool,0> >,...[58]' being compiled

Trying to add generic class-specific placement new (that is void* operator new(size_t size, std::align_val_t, void*) ) seem possible for VS 2019, but not according to the standard and fails in other compilers.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青萝楚歌 2025-02-18 23:16:23

以下只是一个解决方案,它并不总是有效 - 但这很琐碎:

class MyClass : public BaseClass {
public:
  MyClass(int);

  MyClass()=delete;
  MyClass(const MyClass&)=delete;
}

其起作用的原因是Qmetatype通过std :: IS_DEFAULT_CONSTRUCTIBLE_Vstd守护了有问题的调用:: is_copy_constructible_v

The following is just a work-around, and it does not always work - but it is trivial:

class MyClass : public BaseClass {
public:
  MyClass(int);

  MyClass()=delete;
  MyClass(const MyClass&)=delete;
}

The reason it works is that QMetaType has guarded the problematic calls by std::is_default_constructible_v and std::is_copy_constructible_v.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文