【发布时间】:2019-01-15 09:14:32
【问题描述】:
下面是一个简单的应用程序,如果我取消注释 main.cc 中提到的行,它会为我导致 SIGFPE。
config.h
#ifndef SRC_CONFIG_H_
#define SRC_CONFIG_H_
#include <cstdint>
#include <unordered_map>
#include <string>
#include <tuple>
#include <vector>
using ConfigTable_t = std::unordered_map<uint16_t, std::tuple<std::string, std::vector<uint8_t> > >;
static const ConfigTable_t gTable1 {
{ 0x100, std::make_tuple( "table1", std::vector<uint8_t> { 5,5,5,5,5,5 } ) }
};
static const ConfigTable_t gTable2 {
{ 0x200, std::make_tuple( "table2", std::vector<uint8_t> { 0,1,2,3,4,5 } ) }
};
const ConfigTable_t & getConfigTable();
#endif
table_provider.cc
#include "config.h"
const ConfigTable_t & getConfigTable() {
return gTable1;
}
main.cc
#include "config.h"
static const uint16_t gId = 0x100;
// static const std::string gName = std::get<0>(getConfigTable().at(gId)); // <-- Doesn't work
static const std::string gName = std::get<0>(gTable1.at(gId)); // <-- Works
int main() {
return 0;
}
https://stackoverflow.com/a/36406774/3884862 中有一个与此问题相关的指针,但我无法弄清楚它为什么会发生。
我用
编译g++ -std=c++14 main.cc table_provider.cc -o test
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
【问题讨论】:
标签: c++ linker c++14 std unordered-map