| 1 |
// { dg-do compile } |
| 2 |
// { dg-options "-O3 -fdump-tree-optimized -std=c++14" } |
| 3 |
// { dg-skip-if "requires hosted libstdc++ for stdexcept" { ! hostedlib } } |
| 4 |
|
| 5 |
#include <array> |
| 6 |
#include <utility> |
| 7 |
#include <stdexcept> |
| 8 |
|
| 9 |
|
| 10 |
template<class Key, class T, size_t N> struct static_map |
| 11 |
{ |
| 12 |
using key_type = Key; |
| 13 |
using mapped_type = T; |
| 14 |
using value_type = std::pair<const key_type, mapped_type>; |
| 15 |
private: |
| 16 |
using _value_type = std::pair<size_t, value_type>; |
| 17 |
_value_type _values[N]; |
| 18 |
static constexpr _value_type _new_value_type(const std::pair<Key, T> &v) |
| 19 |
{ |
| 20 |
return std::make_pair(0, std::make_pair(v.first, v.second)); |
| 21 |
} |
| 22 |
public: |
| 23 |
template<class... U> constexpr static_map(U &&...il) : _values{ _new_value_type(il)... } { } |
| 24 |
constexpr mapped_type &operator[](const key_type &k) { return at(k); } |
| 25 |
constexpr const mapped_type &operator[](const key_type &k) const { return at(k); } |
| 26 |
constexpr mapped_type &at(const key_type &k) |
| 27 |
{ |
| 28 |
for (size_t n = 0; n < N; n++) |
| 29 |
if (_values[n].second.first == k) |
| 30 |
return _values[n].second.second; |
| 31 |
throw std::out_of_range("Key not found"); |
| 32 |
} |
| 33 |
constexpr const mapped_type &at(const key_type &k) const |
| 34 |
{ |
| 35 |
for (size_t n = 0; n < N; n++) |
| 36 |
if (_values[n].second.first == k) |
| 37 |
return _values[n].second.second; |
| 38 |
throw std::out_of_range("Key not found"); |
| 39 |
} |
| 40 |
}; |
| 41 |
namespace detail |
| 42 |
{ |
| 43 |
template<class Key, class T, size_t N, size_t... I> constexpr static_map<Key, T, N> static_map_from_array(const std::pair<Key, T>(&il)[N], std::index_sequence<I...>) |
| 44 |
{ |
| 45 |
return static_map<Key, T, N>(il[I]...); |
| 46 |
} |
| 47 |
} |
| 48 |
template<class Key, class T, size_t N> constexpr static_map<Key, T, N> make_static_map(const std::pair<Key, T> (&il)[N]) |
| 49 |
{ |
| 50 |
return detail::static_map_from_array<Key, T, N>(il, std::make_index_sequence<N>()); |
| 51 |
} |
| 52 |
|
| 53 |
/* Two phase construction, required because heterogeneous braced init |
| 54 |
in C++ 14 has a big limitation: template<class... Args> auto make(Args &&...) |
| 55 |
will accept make({ 5, "apple" }) as make(int, const char *) but |
| 56 |
make({ 5, "apple" }, { 8, "pear" }) will fail to deduce Args as a |
| 57 |
heterogeneous initializer_list is not permitted. This forces something |
| 58 |
like make(make_pair{ 5, "apple" }, make_pair{ 8, "pear" }, ...) which |
| 59 |
is less succinct than using a constexpr C array for the nested braced init. |
| 60 |
*/ |
| 61 |
constexpr std::pair<const int, const char *> map_data[] = { |
| 62 |
{ 5, "apple" }, |
| 63 |
{ 8, "pear" }, |
| 64 |
{ 0, "banana" } |
| 65 |
}; |
| 66 |
|
| 67 |
template<size_t N> constexpr int cstrcmp(const char *a, const char *b) |
| 68 |
{ |
| 69 |
for (size_t n = 0; n < N; n++) |
| 70 |
{ |
| 71 |
if (a[n] < b[n]) return -1; |
| 72 |
if (a[n] > b[n]) return 1; |
| 73 |
} |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
int main(void) |
| 78 |
{ |
| 79 |
constexpr auto cmap = make_static_map(map_data); |
| 80 |
// No abort() appears in assembler, so this was executed constexpr |
| 81 |
if(!cmap[8]) abort(); |
| 82 |
// This however does cause code implementing a lookup to be generated, |
| 83 |
// so this was NOT executed constexpr |
| 84 |
//const char *foo=cmap[5]; |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
// { dg-final { scan-tree-dump-not "cmap" "optimized" { target x86_64-*-* i?86-*-* } } } |