1 // PR c++/53792 - [C++11] improving compiler-time constexpr evaluation
2 // { dg-do compile { target c++11 } }
3 // { dg-additional-options "-O1 -fdump-tree-optimized" }
4 
5 struct entry
6 {
7   char const* label;
8   int value;
9 };
10 
same(char const * x,char const * y)11 constexpr bool same (char const *x, char const *y)
12 {
13   return !*x && !*y ? true : /* default */ (*x == *y && same (x + 1, y + 1));
14 }
15 
16 constexpr int
keyToValue(char const * label,entry const * entries)17 keyToValue (char const *label, entry const *entries)
18 {
19   return !entries->label ? entries->value
20                          : same (entries->label, label) ? entries->value
21                          : /* default */ keyToValue (label, entries + 1);
22 }
23 
24 constexpr entry foo[] = {{"Foo", 0}, {"Bar", 1}, {"FooBar", 2}, {0, -1}};
25 
bar()26 int bar ()
27 {
28   int result = keyToValue ("Foo", foo);
29   return result;
30 }
31 
baz()32 int baz ()
33 {
34   constexpr int result = keyToValue ("Foo", foo);
35   return result;
36 }
37 
38 // Verify that the call to the keyToValue() constexpr function is inlined
39 // regardless of whether or not it's invoked in a constexpr expression.
40 // { dg-final { scan-tree-dump-not "keyToValue" "optimized" } }
41