首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Qt6和类特定操作符新建编译问题

使用Qt6和类特定操作符新建编译问题
EN

Stack Overflow用户
提问于 2022-06-29 11:37:16
回答 1查看 77关注 0票数 1

考虑一个具有自定义运算符的类。

代码语言:javascript
复制
class BaseClass {
  void* operator new(size_t size);
  void operator delete(void* p);
};

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

然后使用MyClass作为Qt中信号和时隙的参数。

使用Qt5可以工作,但Qt6使用VisualStudio2019时会在QMetaType中生成编译错误,方法是无法为MyClass找到新的类特定位置。

VS 2019年编译错误(更改为隐藏特定路径和类名):

代码语言:javascript
复制
...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

尝试为VS 2019年添加特定于泛型类的布局(即void* operator new(size_t size, std::align_val_t, void*) )似乎是可能的,但不符合标准,而且在其他编译器中失败。

EN

回答 1

Stack Overflow用户

发布于 2022-06-29 11:37:16

以下只是一种解决办法--它并不总是起作用--但它是微不足道的:

代码语言:javascript
复制
class MyClass : public BaseClass {
public:
  MyClass(int);

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

它起作用的原因是QMetaType保护了std::is_default_constructible_vstd::is_copy_constructible_v的问题调用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72800769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档