【发布时间】:2015-09-01 15:24:49
【问题描述】:
可能我错过了一些东西,但我找不到任何信号不能采用右值引用的信息。
所以,我有一个带有以下信号声明的类:
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,那么它当然可以工作。
【问题讨论】:
-
你为什么要用右值引用来发信号(并认为他们可能有几个接收器)?
-
嗯。我希望它们避免复制开销,但是,我没有考虑过多个接收器(尽管它们在我的特定应用程序中是不可能的)。好吧,我的错,它们确实是非法的。谢谢。
-
如果您知道它们是非法的,请去掉
&和move。小心。
标签: c++ qt c++11 signals rvalue-reference