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