考虑一个具有自定义运算符的类。
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年编译错误(更改为隐藏特定路径和类名):
...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*) )似乎是可能的,但不符合标准,而且在其他编译器中失败。
发布于 2022-06-29 11:37:16
以下只是一种解决办法--它并不总是起作用--但它是微不足道的:
class MyClass : public BaseClass {
public:
MyClass(int);
MyClass()=delete;
MyClass(const MyClass&)=delete;
}它起作用的原因是QMetaType保护了std::is_default_constructible_v和std::is_copy_constructible_v的问题调用。
https://stackoverflow.com/questions/72800769
复制相似问题