Search code examples
c++qtqstringqtextstreamqtcore

Qt compile error: endl will always evaluate to true


I have the following code snippet that compiles

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);

for (QStringList::Iterator it = commandList.begin(); it != commandList.end(); ++it) {
    out << "Command: " << *it << endl;
}

but always gives me this warning:

test.cpp:87: warning: the address of 'QTextStream& endl(QTextStream&)' will always evaluate as 'true' [-Waddress]

What does it mean and how do I fix it? Since a newline character is printing I assume this is not a namespace issue...


Solution

  • When you are using a binary data stream, it is not a good practice to start inserting new lines. That is one of the main points over a simple QTextStream.

    This code works fine for me without any warning with gcc version 4.8.1 20130725 (prerelease) (GCC)

    main.cpp

    #include <QByteArray>
    #include <QDataStream>
    #include <QIODevice>
    #include <QStringList>
    
    int main()
    {
        QStringList commandList = QStringList() << "foo" << "bar" << "baz";
        QByteArray block;
        QDataStream out(&block, QIODevice::WriteOnly);
    
        for (QStringList::Iterator it = commandList.begin(); it != commandList.end(); ++it)
            out << "Command: " << *it;
    }
    

    Building

    g++ -std=c++11 -Wpedantic -Wall -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core -fPIC main.cpp