可能我遗漏了什么,但我找不到任何信号不能采用右值引用的信息。
所以,我有一个包含以下信号声明的类:
signals:
void messageDecoded(HTDataMsg &&msg);
当我尝试编译它时,出现错误:
moc_htcodec.cpp: In static member function ‘static void HTCodec::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)’:
moc_htcodec.cpp:71:77: error: cannot bind ‘HTDataMsg’ lvalue to ‘HTDataMsg&&’
case 0: _t->messageDecoded((*reinterpret_cast< HTDataMsg(*)>(_a[1]))); break;
^
In file included from moc_htcodec.cpp:9:0:
../hterm_core/htcodec/htcodec.h:59:9: error: initializing argument 1 of ‘void HTCodec::messageDecoded(HTDataMsg&&)’
void messageDecoded(HTDataMsg &&msg);
^
make: *** [moc_htcodec.o] Error 1
并且生成的moc文件中的代码确实是错误的:
void HTCodec::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{
if (_c == QMetaObject::InvokeMetaMethod) {
HTCodec *_t = static_cast<HTCodec *>(_o);
switch (_id) {
case 0: _t->messageDecoded((*reinterpret_cast< HTDataMsg(*)>(_a[1]))); break;
default: ;
}
} else if (_c == QMetaObject::IndexOfMethod) {
int *result = reinterpret_cast<int *>(_a[0]);
void **func = reinterpret_cast<void **>(_a[1]);
{
typedef void (HTCodec::*_t)(HTDataMsg && );
if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&HTCodec::messageDecoded)) {
*result = 0;
}
}
}
}
这种行为是预期的吗?信号采用右值引用是否违法?
如果我将 HTDataMsg &&msg
更改为,比方说,const HTDataMsg &msg
,那么它当然可以工作。
最佳答案
Is it illegal for signals to take rvalue references?
是的。他们采用右值引用是没有意义的,因为接收者的数量是一个非负整数。右值引用只对零个或一个接收者的特殊情况有意义,即使那样,它也需要 Qt 的 C++11-only 版本。
如果您认为这种优化对常见数据类型有意义(通过基准测量和支持您的断言!),它可以在 Qr 5.7 之后实现,因为它需要平台的 C++11 支持。这需要 moc
以及库本身的支持。
关于c++ - Qt 5 : unable to declare signal that takes rvalue reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30873859/