Skip to main content
deleted 204 characters in body
Source Link
smerlin
  • 6.6k
  • 4
  • 39
  • 58

If you use Qt and endlendl, you could accidentally use the wrongend up using an incorrect endl, happened to me today and i was like . which gives you very surprising results.WTF ?? See the following code snippet:

#include <iostream>
#include <QtCore/QtCore> 
#include <QtGui/QtGui> 

// notice that i dontthere haveis ano "using namespace std;"
int main(int argc, char** argv)
{
    QApplication qapp(argc,argv);
    QMainWindow mw;
    mw.show();
    std::cout << "Finished Execution !" << endl << "...";endl;
    // LineThis aboveprints printedsomething similar to: "Finished Execution !67006AB4..."67006AB4"
    return qapp.exec();
}

Of courseNote that was my mistake, since i should have writtenI wrote endl instead of std::endl, but if you use endl, qt and using namespace std; it depends on the order of the include files if the correct endl will be used.*

Of course you could recompile Qt to use a namespace, so you get(which would have been correct) and apparently there is a compilation error for the example above.

EDIT: Forgot to mention, Qt's endl is declaredfunction defined in "qtextstream.h" whichqtextstream.h (which is part of QtCore).

*EDIT2: C++ will pick the correct endl if you have a using for std::cout or the namespaceUsing std"\n", since instead of std::endl completely sidesteps any potential namespace issues. This is inalso a good example why putting symbols into the sameglobal namespace as std::cout, C++'s ADL mechanism will pick std::endl(like Qt does by default) is a bad idea.

If you use Qt and endl, you could accidentally use the wrong endl, happened to me today and i was like ..WTF ??

#include <iostream>
#include <QtCore/QtCore> 
#include <QtGui/QtGui>
//notice that i dont have a "using namespace std;"
int main(int argc, char** argv)
{
    QApplication qapp(argc,argv);
    QMainWindow mw;
    mw.show();
    std::cout << "Finished Execution !" << endl << "...";
    // Line above printed: "Finished Execution !67006AB4..."
    return qapp.exec();
}

Of course that was my mistake, since i should have written std::endl, but if you use endl, qt and using namespace std; it depends on the order of the include files if the correct endl will be used.*

Of course you could recompile Qt to use a namespace, so you get a compilation error for the example above.

EDIT: Forgot to mention, Qt's endl is declared in "qtextstream.h" which is part of QtCore

*EDIT2: C++ will pick the correct endl if you have a using for std::cout or the namespace std, since std::endl is in the same namespace as std::cout, C++'s ADL mechanism will pick std::endl.

If you use Qt and endl, you could accidentally end up using an incorrect endl which gives you very surprising results. See the following code snippet:

#include <iostream>
#include <QtCore/QtCore> 
#include <QtGui/QtGui> 

// notice that there is no "using namespace std;"
int main(int argc, char** argv)
{
    QApplication qapp(argc,argv);
    QMainWindow mw;
    mw.show();
    std::cout << "Finished Execution!" << endl;
    // This prints something similar to: "Finished Execution!67006AB4"
    return qapp.exec();
}

Note that I wrote endl instead of std::endl (which would have been correct) and apparently there is a endl function defined in qtextstream.h (which is part of QtCore).

Using "\n" instead of endl completely sidesteps any potential namespace issues. This is also a good example why putting symbols into the global namespace (like Qt does by default) is a bad idea.

clarified some incorrect statements
Source Link
smerlin
  • 6.6k
  • 4
  • 39
  • 58

If you use Qt and endl, you could accidentally use the wrong endl, happened to me today and i was like ..WTF ??

#include <iostream>
#include <QtCore/QtCore> 
#include <QtGui/QtGui>
//notice that i dont have a "using namespace std;"
int main(int argc, char** argv)
{
    QApplication qapp(argc,argv);
    QMainWindow mw;
    mw.show();
    std::clogcout << "Finished Execution !" << endl << "...";
    // Line above printed: "Finished Execution !67006AB4..."
    return qapp.exec();
}

Of course that was my mistake, since i should have written std::endl, but if you use endl, qt and using namespace std; it depends on the order of the include files if the correct endl will be used.but if you use endl, qt and using namespace std; it depends on the order of the include files if the correct endl will be used.*

Of course you could recompile Qt to use a namespace, so you get a compilation error for the example above.

EDIT: Forgot to mention, Qt's endl is declared in "qtextstream.h" which is part of QtCore

*EDIT2: C++ will pick the correct endl if you have a using for std::cout or the namespace std, since std::endl is in the same namespace as std::cout, C++'s ADL mechanism will pick std::endl.

If you use Qt and endl, you could accidentally use the wrong endl, happened to me today and i was like ..WTF ??

#include <iostream>
#include <QtCore/QtCore> 
#include <QtGui/QtGui>
//notice that i dont have a "using namespace std;"
int main(int argc, char** argv)
{
    QApplication qapp(argc,argv);
    QMainWindow mw;
    mw.show();
    std::clog << "Finished Execution !" << endl << "...";
    // Line above printed: "Finished Execution !67006AB4..."
    return qapp.exec();
}

Of course that was my mistake, since i should have written std::endl, but if you use endl, qt and using namespace std; it depends on the order of the include files if the correct endl will be used.

Of course you could recompile Qt to use a namespace.

EDIT: Forgot to mention, Qt's endl is declared in "qtextstream.h" which is part of QtCore

If you use Qt and endl, you could accidentally use the wrong endl, happened to me today and i was like ..WTF ??

#include <iostream>
#include <QtCore/QtCore> 
#include <QtGui/QtGui>
//notice that i dont have a "using namespace std;"
int main(int argc, char** argv)
{
    QApplication qapp(argc,argv);
    QMainWindow mw;
    mw.show();
    std::cout << "Finished Execution !" << endl << "...";
    // Line above printed: "Finished Execution !67006AB4..."
    return qapp.exec();
}

Of course that was my mistake, since i should have written std::endl, but if you use endl, qt and using namespace std; it depends on the order of the include files if the correct endl will be used.*

Of course you could recompile Qt to use a namespace, so you get a compilation error for the example above.

EDIT: Forgot to mention, Qt's endl is declared in "qtextstream.h" which is part of QtCore

*EDIT2: C++ will pick the correct endl if you have a using for std::cout or the namespace std, since std::endl is in the same namespace as std::cout, C++'s ADL mechanism will pick std::endl.

Source Link
smerlin
  • 6.6k
  • 4
  • 39
  • 58

If you use Qt and endl, you could accidentally use the wrong endl, happened to me today and i was like ..WTF ??

#include <iostream>
#include <QtCore/QtCore> 
#include <QtGui/QtGui>
//notice that i dont have a "using namespace std;"
int main(int argc, char** argv)
{
    QApplication qapp(argc,argv);
    QMainWindow mw;
    mw.show();
    std::clog << "Finished Execution !" << endl << "...";
    // Line above printed: "Finished Execution !67006AB4..."
    return qapp.exec();
}

Of course that was my mistake, since i should have written std::endl, but if you use endl, qt and using namespace std; it depends on the order of the include files if the correct endl will be used.

Of course you could recompile Qt to use a namespace.

EDIT: Forgot to mention, Qt's endl is declared in "qtextstream.h" which is part of QtCore