[Qt-interest] Qt code compiles fine on mscv9.0 but failed with gcc
Constantin Makshin
cmakshin at gmail.com
Wed Jan 26 16:39:07 CET 2011
I guess that in MSVC, when you pass a temporary object as a non-const reference and then the function modifies this referenced object, it modifies the temporary object.
Let's assume you have a function:
void someFunc (QString& str)
{
// Modify str in some way
}
If you call it this way:
QString someString;
someFunc(someString);
everything is correct.
But if you call it this way:
someFunc(QString("abc"));
then the only difference from the first example is that any changes are lost when the temporary object is destroyed, i.e. when someFunc() finishes its work.
While this behavior may be wrong from C++ point of view, it has some sense IMHO.
On Wednesday 26 January 2011 14:12:58 Bruno Colombet wrote:
> Le 25/01/2011 18:02, Tim Dewhirst a écrit :
> > Hello,
> >
> > On 25/01/2011 16:18, Bo Thorsen wrote:
> >> Den 25-01-2011 16:51, jjDaNiMoTh skrev:
> >>> 2011/1/25 Bob Hood<bhood2 at comcast.net>:
> >>>> On 1/25/2011 6:24 AM, Bruno Colombet wrote:
> >>>>> a call like this : addCustomChannel(chan) compiles in MSCV
> >>>>> but gcc complains: error: default argument parameter of type 'QString&' has
> >>>>> type 'QString'
> >>>>>
> >>>> Yeah, welcome to the nightmare of modern cross compiling. :)
> >> I completely disagree with that (and it's multi compiler, not cross
> >> compiler). The more time you spend on multiple compilers, the cleaner
> >> C++ you write.
> > I completely agree with that; I've lost count of the number of times
> > we've gone to work on someone's codebase to discover they've been using
> > e.g. msvc9 exclusively with loads of warnings turned off, and then
> > wonder why their code won't compile first time with a different
> > compiler/platform/...
> >
> > Tim
> > _______________________________________________
> > Qt-interest mailing list
> > Qt-interest at qt.nokia.com
> > http://lists.qt.nokia.com/mailman/listinfo/qt-interest
> You're right Tim but I was assuming msvc compiler was doing the
> correction automaticaly and I think that is actually what it does. Gcc
> doesn't and then came the pb.
> Even with no warnings about const reference type, msvc produced a code
> that is fully functionnal and stable.
>
> I agree anyway that we must pay attention to stay in the correct
> programming way of C++.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110126/5cf8e969/attachment.bin
More information about the Qt-interest-old
mailing list