@@ -4006,7 +4006,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict,
return false;
if (t == NULL_TREE)
return true;
- if (TREE_THIS_VOLATILE (t))
+ if (TREE_THIS_VOLATILE (t) && (EXPR_P (t) || want_rval))
{
if (flags & tf_error)
error ("expression %qE has side-effects", t);
new file mode 100644
@@ -0,0 +1,29 @@
+// PR c++/67333
+// { dg-do compile { target c++11 } }
+
+template <int N>
+struct integral_constant
+{
+ static constexpr int value = N;
+};
+
+template <typename T, int S>
+constexpr int lengthof (const volatile T (&)[S])
+{
+ return S;
+}
+
+template <typename T, int S>
+constexpr int valueof (const volatile T (&s)[S]) // { dg-error "has side-effects" }
+{
+ return s[0];
+}
+
+int main ()
+{
+ volatile int meow[4] {};
+ integral_constant<lengthof (meow)>::value; // OK
+ integral_constant<valueof (meow)>::value; // { dg-error "in a constant expression" }
+ return 0;
+}
+
new file mode 100644
@@ -0,0 +1,36 @@
+// PR c++/67333
+// { dg-do compile { target c++14 } }
+
+template <int N>
+struct integral_constant
+{
+ static constexpr int value = N;
+};
+
+constexpr int decl (int x)
+{
+ volatile int v = x;
+ return x;
+}
+
+constexpr int use (int x)
+{
+ volatile int v = x;
+ return v;
+} // { dg-error "has side-effects" }
+
+constexpr int test_ref (volatile int &x)
+{
+ volatile int &vol_ref = x;
+ volatile int *vol_ptr = &x;
+ volatile int &vol_deref = *vol_ptr;
+ return 0;
+}
+
+int main()
+{
+ volatile int x = 0;
+ constexpr int t = test_ref (x);
+ integral_constant<decl (2)>::value; // OK
+ integral_constant<use (2)>::value; // { dg-error "in a constant expression" }
+}