使用返回初始化列表中的临时函数是否合法

时间:2012-01-17 17:14:59

标签: c++ qt maemo

我有一个对象的构造函数

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

mOrigin类型为Vector3df,函数toVector3df(QPointF const&)返回临时Vector3df。到现在为止还挺好。代码编译很好,就像linux下的魅力一样,gcc 4.4.3。大多数警告已激活。

现在我想为诺基亚智能手机(Meamo Fremantle)交叉编译相同的代码 突然间,我得到了非常奇怪的编译器警告:

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

第一:当然在'Vecto3df'中没有名为this.902的真正变量,所以我的第一个问题是:“有没有人看到过这样的警告?”此外,Vector3df构造函数没有任何问题,它们非常简单,toVector3df(QPointF const&)是一个单行非成员模板函数,在代码的其他部分工作得很好。 Vector3df继承自仅定义非成员函数的模板,没有变量no,虚函数。

其次,当我将上述代码更改为以下

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

代码工作正常,没有任何警告。 那我在这里错过了什么?有谁知道警告的起源是什么。我是否违反了一些我不知道的学说。 fremantle编译器(Maemo 5,Qt 4.6.2)是更严重还是错误?

先谢谢,马丁

编辑: 这是一个最小的例子,抱歉长度:-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;
}

编译器调用:

 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

模板继承似乎至关重要,如果Vector3d没有继承一切正常。

1 个答案:

答案 0 :(得分:3)

使用在成员初始化列表中返回临时函数的函数没有任何问题 甚至成员将被初始化的顺序也在标准中明确定义。