Boost logo

Boost :

Subject: Re: [boost] [gsoc16] Can I quickly check if the below really is the best approach?
From: Shangtong Zhang (zhangshangtong.cpp_at_[hidden])
Date: 2016-01-16 02:06:48


Hi,

> // At global scope, allocates no memory whatsoever
> static const constexpr static_map<int, const char *> 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.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk