【发布时间】:2018-07-20 13:08:55
【问题描述】:
我有一个实现中值过滤器的类模板。模板参数是数据类型和过滤器长度。
使用基于 gcc 4.8.3 的 arm-none-eabi-g++ 进行编译时出现段错误。使用 Clang 5.0.1 或 Gcc 7.3.0 编译代码时不会发出警告。
我正在为 c++11 编译。
是我的代码有问题,还是真的是 gcc 错误?如果是错误,是否有方便的解决方法?即使不是错误,我也会感谢任何改进建议。
我能想到的可能是原因(尽管我认为它们都应该是允许的):
- 在这个类中声明了一个结构,它的成员之一是指向结构类型本身的指针
- 这个类有两个静态constexpr成员数据元素
代码
声明
// In MedianFilter.h
template <typename dType, int len>
class MedianFilter
{
public:
dType read() { return xMed_; }
dType update(dType x);
private:
struct pair
{
pair* nextSmallest;
dType xi;
};
static constexpr dType stopper_ {0};
static constexpr int filterLen_ {len};
pair buffer_[filterLen_] = {};
pair* datpoint = buffer_;
pair small_ = {nullptr, stopper_};
pair big_ = {&small_, stopper_};
dType xMed_ {stopper_};
};
定义(在 .h 文件中)未显示
Main.cpp
#include <cstdint>
using std::uint16_t;
#include "MedianFilter.h"
int main() {
MedianFilter<uint16_t, 7> filt;
filt.update(0);
return 0;
}
错误输出:
In file included from main.cpp:1:0: MedianFilter.h: In constructor
'constexpr MedianFilter<short unsigned int, 7>::MedianFilter()':
MedianFilter.h:22:7: internal compiler error: Segmentation fault
class MedianFilter
Please submit a full bug report, with preprocessed source if appropriate. See <http://gcc.gnu.org/bugs.html> for instructions.
【问题讨论】:
-
“内部编译器错误”总是意味着编译器错误。
-
使用 gcc x86 4.8.3 复制。一个最小示例:godbolt.org/g/H3qSTq(编译器实现者喜欢最小示例,在错误报告中没有
#include指令。) -
感谢您提供的最小示例!我将使用它并提交错误报告。既然你已经查明了这个问题,我会看看我是否可以解决它。