diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 6ce1bb61fe7..439681216be 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -4092,7 +4092,8 @@ cp_build_array_ref (location_t loc, tree array, tree idx, tree ar = cp_default_conversion (array, complain); tree ind = cp_default_conversion (idx, complain); - if (!first && flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind)) + if (!processing_template_decl + && !first && flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind)) ar = first = save_expr (ar); /* Put the integer in IND to simplify error checking. */ diff --git a/gcc/testsuite/g++.dg/cpp1z/eval-order13.C b/gcc/testsuite/g++.dg/cpp1z/eval-order13.C new file mode 100644 index 00000000000..6e8ebeb3096 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/eval-order13.C @@ -0,0 +1,29 @@ +// PR c++/117158 - Similar to eval-order7.C, only with templates. +// { dg-do run { target c++11 } } +// { dg-options "-fstrong-eval-order" } + +int a[4] = { 1, 2, 3, 4 }; +int b[4] = { 5, 6, 7, 8 }; + +struct Base { + int *intarray; +}; + +template +struct Sub : public Base { + int Get(int i) { + Base::intarray = a; + int r = Base::intarray[(Base::intarray = b, i)]; + if (Base::intarray != b) + __builtin_abort (); + return r; + } +}; + +int +main () +{ + Sub s; + if (s.Get (3) != 4) + __builtin_abort (); +} diff --git a/gcc/testsuite/g++.dg/parse/crash77.C b/gcc/testsuite/g++.dg/parse/crash77.C new file mode 100644 index 00000000000..729362eb599 --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/crash77.C @@ -0,0 +1,13 @@ +// PR c++/117158 +// { dg-do "compile" } + +struct Base { + unsigned int *intarray; +}; + +template +struct Sub : public Base { + bool Get(int i) { + return (Base::intarray[++i] == 0); + } +};