Est-il légal d'utiliser des fonctions renvoyant une liste temporaire dans les listes d'initialisation

StackOverflow https://stackoverflow.com/questions/8898830

  •  29-10-2019
  •  | 
  •  

Question

J'ai le constructeur suivant d'un objet

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

mOrigin est de type Vector3df et la fonction toVector3df(QPointF const&) retourne un temporaire Vector3df. Jusqu'ici tout va bien. Le code compile bien et fonctionne comme un charme sous Linux, GCC 4.4.3. La plupart des avertissements se sont activés.

Maintenant, je voulais transformer le même code pour un smartphone Nokia (Meamo Fremantle) et tout d'un coup, je reçois des avertissements de compilateur très étranges:

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

Premièrement: bien sûr, il n'y a pas de variable réelle appelée ceci.902 à l'intérieur de «Vecto3df», donc ma première question serait: "Quelqu'un a-t-il vu un avertissement comme ça?" De plus, il n'y a rien de mal à Vector3df Constructeurs, ils sont très simples et toVector3df(QPointF const&) est une fonction de modèle de non-membre One Linner qui fonctionne parfaitement dans d'autres parties du code.Vector3df Hérite d'un modèle qui ne définit que les fonctions non membres, pas de variables non, fonctions virtuelles.

Deuxièmement, lorsque je change le code ci-dessus en ce qui suit

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

Le code fonctionne bien sans aucun avertissement. Alors, qu'est-ce que je manque ici? Quelqu'un a une idée de l'origine des avertissements. Est-ce que je viole une doctrine dont je ne suis pas au courant. Le compilateur Fremantle (Maemo 5, Qt 4.6.2) est-il plus grave ou buggy?

Merci d'avance, Martin

Edit: voici un exemple minimal, désolé pour la longueur: 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(p.x(),p.y(),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;
}

Appel du compilateur:

 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

L'héritage du modèle semble être crucial, si le vector3d n'hérite pas que tout fonctionne bien.

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top