g++ 4.8.2坚持简单变量成员是数组

G++ 4.8.2 insists simple variable members are arrays

本文关键字:变量 成员 数组 简单 坚持 g++      更新时间:2023-10-16

此问题仅发生在g++ 4.8.2 for ARMv6 (stock pidora);它在x86_64 w/clang 3.4.2和g++ 4.8.3上编译时没有错误或警告。我很难不把它看作是一个编译器的错误,但想得到一些其他的意见。

它涉及到一个简单的成员变量,g++一直坚持它是一个数组和

error: array must be initialized with a brace-enclosed initializer

类的头看起来像这样:

namespace SystemStateMonitor {
class ramInput : public input, public inputFile {
        public:
                typedef enum {
                        RATIO,
                        PERCENT,
                        KiBYTES,
                        MiBYTES
                } style_t;
                ramInput (
                        const std::string &label = "RAM",
                        style_t style = style_t::PERCENT
                );
                unsigned int getAvailable ();
                double getDV ();
                double ratio ();
        protected:
                style_t style;
                unsigned int available;
                void setStyle (style_t);
        friend input* jsonInputRAM (jsonObject);
};
}

构造函数如下:

#define PROC_FILE "/proc/meminfo"
using namespace std;
using namespace SystemStateMonitor;
ramInput::ramInput (
        const string &label,
        ramInput::style_t s
) :
        input (label),
        inputFile (PROC_FILE),
        style (s),
        available (0)
{
        setStyle(style);
}

当我用ARMv6 g++编译它时,我得到:

inputs/ramInput.cpp:19:14: error: array must be initialized with a brace-enclosed initializer
  available (0)
              ^

父类没有任何成员"available";没有潜在的奇怪碰撞。有趣的是,如果我修改构造函数:

) :
        input (label),
        inputFile (PROC_FILE),
        style (s)
{
        available = 0;
        setStyle(style);
}

对于style (s),我现在得到相同的错误。如果我对style做同样的事情(将初始化移动到主体中),我得到inputFile (PROC_FILE)的错误,更奇怪,因为这是一个超级构造函数调用。

inputs/ramInput.cpp:17:22: error: array must be initialized with a brace-enclosed initializer
  inputFile (PROC_FILE)
                      ^

不幸的是,但并不奇怪,SSCCE以这个开头:

class test {
        public:
                test () : x(0) { };
                unsigned int x;
};

不能重现问题。

这里可能出了什么问题?我能相信这不是我的错吗?

正如Mike Seymour在对问题的评论中指出的那样,错误的级联性质表明编译器只是指向问题初始化列表的末尾,而不是指向正确的条目。该数组位于超类构造函数默认初始化项中:

   std::array<double,2> range = { 0 }

这个特殊的g++阻塞在上面,并且:

   std::array<double,2> range = { 0.0, 0.0 }

但:

   std::array<double,2> range = { }

。还好我不想要任何非零值