Boost logo

Boost :

Subject: [boost] Static constexpr map (was: Re: [gsoc16] Can I quickly check if the below really is the best approach?)
From: Shangtong Zhang (zhangshangtong.cpp_at_[hidden])
Date: 2016-01-18 08:17:38

Hi Niall Douglas,

> If it is constexpr, it can only be modified
> in a constexpr evaluation context, and otherwise not.

When a variable is declared as constexpr, it seems it’s also const,

constexpr int foo() {
  constexpr int i = 5;
  i = 6;
  return i;

int main () {
  constexpr int j = foo();
  return 0;

foo is a constexpr evaluation context, but modification of i in it will still leads to compile error.

> • Values, though not keys nor number of items, are modifiable.

So I think this feature is also challenging.
In your crack, <>,
if I add
cmap[0] = “orange”;
in main function, it will leads to compile error.

> // Challenging: needs to only generate code loading immediately from a memory location.
> // It must NOT generate any additional runtime overhead like hashing nor searching.
> const char *what_is_8 = cmap[8];

I find some macro which can force constexpr function evaluation
(Add it in your crack)

#define AT_CONSTEXPR_KEY(_MAP, _KEY) [&_MAP, &_KEY](){constexpr auto value = _MAP[_KEY]; return value;}()
#define AT_TEMP_KEY(_MAP, _KEY) [&_MAP](){constexpr auto value = _MAP[_KEY]; return value;}()
constexpr int key = 5;
const char* foo1 = AT_CONSTEXPR_KEY(cmap, key);
const char* foo2 = AT_TEMP_KEY(cmap, 8);

It indeed generates no additional code, but I don’t know how to unify these two macros.
In addition, following expression will leads to compile error as lambda function can’t be constexpr now.

constexpr const char* foo1 = AT_CONSTEXPR_KEY(cmap, key);
constexpr const char* foo2 = AT_TEMP_KEY(cmap, 8);

Shangtong Zhang,
Senior Student,
School of Computer Science,
Fudan University, PRC.

Boost list run by bdawes at, gregod at, cpdaniel at, john at