Is it legal to use functions that return temporary initializer lists

I have the following object constructor

Segment::Segment(QPointF const& start, QPointF const& end): mOrigin(toVector3df(start)),mEnd(toVector3df(end)){ } 

mOrigin is of type Vector3df , and the toVector3df(QPointF const&) function toVector3df(QPointF const&) returns a temporary Vector3df . So far, so good. The code compiles fine and works like charm under linux, gcc 4.4.3. most alerts activated.

Now I wanted to compile the same code for a Nokia smartphone (Meamo Fremantle) and suddenly I get very strange compiler warnings:

 include/vector3d.h: In constructor 'Segment::Segment(const QPointF&, const QPointF&)': include/vector3d.h:64: warning: 'this.902' is used uninitialized in this function include/vector3d.h:64: note: 'this.902' was declared here 

First: Of course, there is no real variable called this.902 inside 'Vecto3df', so my first question is: โ€œHas anyone seen such a warning?โ€ Vector3df is nothing wrong with the Vector3df constructors, they are very simple, and toVector3df(QPointF const&) is one of the template functions that is not a member and works fine in other parts of the code. Vector3df inherits from a template that defines only functions that are not members, no variables, no virtual functions.

Secondly, when I change the above code to the following

 Segment::Segment(QPointF const& start, QPointF const& end): mOrigin(),mEnd(){ mOrigin = toVector3df(start); mEnd = toVector3df(end); } 

The code works fine without any warnings. So what am I missing here? Does anyone know what the origin of warnings is. I break some doctrines that I donโ€™t know about. Is the fremantle compiler (Maemo 5, Qt 4.6.2) more serious or buggy?

Thanks in advance, Martin

Edit: Here is a minimal example, sorry for the length: -P

 #include <iostream> #include <sstream> #include <QPoint> template<typename T> class IoEnabled {}; template<typename T> class Vector3d: public IoEnabled<Vector3d<T> > { private: T mX; T mY; T mZ; public: Vector3d(T const& x, T const& y, T const& z=0.0) : mX(x), mY(y), mZ(z) {} }; typedef Vector3d<float> Vector3df; template<class T> Vector3df toVector3df(T const& p){ return Vector3df(px(),py(),0.0); } class Segment { private: Vector3df mOrigin; Vector3df mEnd; public: Segment(QPointF const& start, QPointF const& end): mOrigin(toVector3df(start)),mEnd(toVector3df(end)){ //if toVector3df(...) is moved from the initializer to the body it works } }; int main(int argc, char **argv) { (void) argc; (void) argv; Segment temp(QPointF(1,2),QPointF(3,4)); return 0; } 

Compiler call:

  g++ -c -pipe -Werror -Wall -Wextra -Wunused -Wundef -Wpointer-arith -Wcast-align -Wwrite-strings -Wredundant-decls -O3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -D_REENTRANT -Wall -W -DQT_GL_NO_SCISSOR_TEST -DQT_DEFAULT_TEXTURE_GLYPH_CACHE_WIDTH=1024 -DMAEMO -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/opt/QtSDK/Maemo/4.6.2/sysroots/fremantle-arm-sysroot-20.2010.36-2-slim/usr/share/qt4/mkspecs/linux-g++-maemo5 -I. -I/opt/QtSDK/Maemo/4.6.2/sysroots/fremantle-arm-sysroot-20.2010.36-2-slim/usr/include/QtCore -I/opt/QtSDK/Maemo/4.6.2/sysroots/fremantle-arm-sysroot-20.2010.36-2-slim/usr/include/QtGui -I/opt/QtSDK/Maemo/4.6.2/sysroots/fremantle-arm-sysroot-20.2010.36-2-slim/usr/include -Isrc -Irelease/moc -o release/obj/main.o src/main.cpp 

Template inheritance seems to be crucial if Vector3d doesn't inherit, everything works fine.

+4
source share
1 answer

There is nothing wrong with using functions that return temporary lists of member initializers.
Even the order in which members will be initialized is well defined in the standard.

+3
source

Source: https://habr.com/ru/post/1391504/


All Articles