Boost logo

Boost :

Subject: [boost] Static constexpr map (was: Re: [gsoc16] Can I quickly check if the below really is the best approach?)
From: Niall Douglas (s_sourceforge_at_[hidden])
Date: 2016-01-13 14:43:58


On 13 Jan 2016 at 13:25, Lee Clagett wrote:

> > Can I quickly check here if that claim is true? Is there a better way
> > than my example program?
>
> AFAIK your code is the current best practice for generating a constant
> static map with runtime lookups

Cool, thanks for having a look.

> > [1]: I believe Boost.Hana could implement a static constan
> > associative map quite easily, but I'd be fairly sure Hana will likely
> > be beyond most students.
> >
>
> If using Hana is beyond most GSoC students, it seems like writing a
> `constexpr` associative map will be too. For example, computing the
> amount of storage space needed for the map must be done _before_
> entering the constexpr function in its return type, which I think is
> harder to grasp than the purely runtime components of Hana. And if the
> student can understand that level of TMP, then Hana should not be an
> issue.

All absolutely true, but you're forgetting the time it takes to
master a Boost library and a Boost/STL idiomatic practice. As a
general rule, if they are extending a Boost library they just about
get going by the end of the summer after a lot of help, whereas if
it's new code you tend to see more get accomplished because they are
more psychologically invested for them to build their own thing
rather than build atop someone else's thing (despite that the latter
is almost certainly better for them in terms of training).

> > #define STRING_VIEW(str) { str, sizeof(str)-1 }
> > constexpr std::initializer_list<std::pair<const string_view,
> > weekday>> string_to_weekday {
> > { STRING_VIEW("sunday"), weekday::sunday },
> > { STRING_VIEW("monday"), weekday::monday },
> > { STRING_VIEW("tuesday"), weekday::tuesday },
> > { STRING_VIEW("wednesday"), weekday::wednesday },
> > { STRING_VIEW("thursday"), weekday::thursday },
> > { STRING_VIEW("friday"), weekday::friday },
> > { STRING_VIEW("saturday"), weekday::saturday }
> > };
> >
>
> The macro `STRING_VIEW` seems unnecessary because the `string_view`
> constructor taking a single NULL-terminated string is also
> `constexpr`. Although, it could reduce the amount of computation
> done by the C++ interpreter in the compiler.

Sadly, the current proposal for string_view does not provide a
default Traits length function which is constexpr for const char *.
Yes, I find that very daft as a default design choice for a
string_view, but the macro at least makes it not too irritating.

Niall

-- 
ned Productions Limited Consulting
http://www.nedproductions.biz/ 
http://ie.linkedin.com/in/nialldouglas/



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