diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index d19d09adde43b713b6c4149f0c3c4ed901f9d3d1..05e4600c7bb9c61499af1579744a5962b55d48a3 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -15259,7 +15259,12 @@ grokdeclarator (const cp_declarator *declarator, /* Record constancy and volatility on the DECL itself . There's no need to do this when processing a template; we'll do this for the instantiated declaration based on the type of DECL. */ - if (!processing_template_decl) + if (!processing_template_decl + /* Don't do it for instantiated variable templates either, + cp_apply_type_quals_to_decl should have been called on it + already and might have been overridden in cp_finish_decl + if initializer needs runtime initialization. */ + && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl))) cp_apply_type_quals_to_decl (type_quals, decl); return decl; diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ87.C b/gcc/testsuite/g++.dg/cpp1y/var-templ87.C new file mode 100644 index 0000000000000000000000000000000000000000..e62d06d172c53d19ba16e5d0638f32e942a486c8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ87.C @@ -0,0 +1,43 @@ +// PR c++/113976 +// { dg-do run { target c++14 } } + +int +foo () +{ + return 42; +} + +template <int N> +const int a = foo (); +const int *b = &a <0>; +template <int N> +const int c = foo (); +template const int c <0>; +template <int N> +const int d = foo (); +const int *e = &d <0>; +template const int d <0>; +template <int N> +const int f = foo (); +template const int f <0>; +const int *g = &f <0>; +struct S { int a, b; }; +template <int N> +const S h = { 42, foo () }; +const S *i = &h <0>; +template <int N> +const S j = { 42, foo () }; +template const S j <0>; +template <int N> +const S k = { 42, foo () }; +const S *l = &k <0>; +template const S k <0>; +template <int N> +const S m = { 42, foo () }; +template const S m <0>; +const S *n = &m <0>; + +int +main () +{ +}