C++ Template Question

Olivier Goffart ogoffart at kde.org
Thu Aug 23 00:42:41 BST 2007


Le jeudi 23 août 2007, Adriaan de Groot a écrit :
> On Thursday 23 August 2007 00:24:11 Olivier Goffart wrote:
> > Le lundi 20 août 2007, Mosfet a écrit :
> > > http://doc.trolltech.com/4.3/qvarlengtharray.html
> >
> >  int myfunc(int n)
> >  {
> >      int table[n + 1];  // WRONG
>
> Very much wrong indeed.

Wrong according ISO C++,   but not according GCC

>
> > This works fine with GCC (unless you use --pedantic)
> > It's a nice gnu extention.
> > Does it works with others compiler supported by KDE?
>
> Nope. At least one doesn't like it.

Which compiler are you talking about ?

> > allow arrays dynamically on the stack if the compiler is GCC, and use
> > QVarLengthArray otherwise? Would that be worth?
>
> You would have be be quite careful that *everything* you do is portable on
> both sides; I can imagine weird cases like returning pointers to elements
> or returning the entire array that might work on the QVarLengthArray and
> not on the GCC-istic variable length array.

Yes, of course, you can't return a pointer to something which is allocated on 
the stack,  but this is not specific to dynamic array.

And, even if you can return a QVarLengthArray  I would not recommend it since 
it will do a deep copy. QVector should be used in that case.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20070823/8f9a47ce/attachment.sig>


More information about the kde-core-devel mailing list