LoginSignup
3

More than 3 years have passed since last update.

posted at

updated at

C++14 or later で constexpr で std::map 的にリードオンリー連想配列を作るメモ

背景

std::map<std::string, int> みたいなシグネチャの動的に内容変化せずリードオンリーの連想配列(変換テーブルなど)を作りたい需要はよくありますが,

constexpr std::map<std::string, int> ...

というのは直接にはできません.

ライブラリ

C++14 以降になります.

frozen 使ってみましたが, いい感じに動きました. frozen::stringstd::string.c_str() はないので, 使うにはちょっと変換いります(.data()const char * ポインタが取得できるのでこれを使う)

デカコーンと言われる mapbox さま謹製の eternal も良さそうです. ありがとうございます.

C++17 で実現

優秀な C++ さまから, C++17(std::string_view)で super minimal constexpr map をお教えいただきました! :tada:

素晴らしいですね, ありがとうございます.

TODO

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
3