Data structure with get returning constexpr (C ++)

I'm currently looking for a data structure that encapsulates data for access at compile time. Thus, the available values ​​should be returned as constexpr.

While the tuple has a constexpr constructor, the get function of the tuple does not return constexpr.

Is there such a data structure or is it possible to manually define such a data structure?

The ultimate goal is to compile a package of known values ​​of time, within an object, pass it (using a template) to a function, access to elements there is time known compilation values ​​directly inserted into binary binary constants by InPlace. For my purpose, the component is crucial.

+4
source share
1 answer

Starting with C ++ 14, std::tuple accepts constexpr std::get

 #include <tuple> int main() { constexpr std::tuple<int, int, int> t { 1, 2, 3 }; static_assert(std::get<0>(t) == 1, ""); } 

Live example

Similarly, you can use std::array also with std::get (as well as operator[] now constexpr ). You can also execute RC arrays.

 #include <array> int main() { constexpr std::array<int, 3> a {{ 1, 2, 3 }}; static_assert(std::get<0>(a) == 1, ""); static_assert(a[0] == 1, ""); } 

Live example

+1
source

All Articles