16 Jan
2016
16 Jan
'16
7:06 a.m.
Hi,
// At global scope, allocates no memory whatsoever static const constexpr static_map
map { { 5, "apple" }, { 8, "pear" }, { 0, "banana" }, ... }; // Works, constexpr "apple" constexpr const char *what_is_5 = map[5]; // Works assert(map[5] && !strcmp(map[5], "apple"); // Works map[5]="orange"; assert(map[5] && !strcmp(map[5], "orange"); // Throws exception as there is no key 6 map[6]; map.at http://map.at/(6);
I have difficulty understanding this, As variable map is const and constexpr, how can map[5]=“orange” work? I don’t know how to implement this, where to store the “orange”. Could anyone help me? Thanks, Shangtong Zhang, Senior Student, School of Computer Science, Fudan University, PRC.