Qt 5:无法声明采用右值参考的信号

Dmi*_*ank 5 c++ qt signals rvalue-reference c++11

可能我错过了一些东西,但我找不到任何信号不能接受右值参考的信息.

所以,我有一个具有以下信号声明的类:

signals:
   void messageDecoded(HTDataMsg &&msg);
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,我遇到了错误:

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
Run Code Online (Sandbox Code Playgroud)

生成的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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这种行为有望吗?信号采用右值参考是否合法?

如果我换HTDataMsg &&msg到,比方说const HTDataMsg &msg,那当然可行.

Rei*_*ica 8

信号采用右值引用是否违法?

是的。它们采用右值引用是没有意义的,因为接收器的数量是一个非负整数。右值引用只对零或一个接收器的特殊情况有意义,即使这样,它也需要一个仅限 C++11 的 Qt 版本。

如果您认为这样的优化对常见数据类型有意义(通过基准测试来衡量和支持您的断言!),它可以在 Qr 5.7 以后实现,因为它需要平台支持 C++11。这需要moc图书馆本身的支持。