Skip to content

Commit 3c88dad

Browse files
Accept template-dependent variable template-ids in C++03 constant expressions [GH #489]
1 parent 4ddcdba commit 3c88dad

10 files changed

Lines changed: 163 additions & 2 deletions

File tree

‎src/exprutil.c‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17760,7 +17760,7 @@ be returned for a C mode const variable.
1776017760
function-local as well). */
1776117761
con_val = initializer_constant(var);
1776217762
if (con_val == NULL && gpp_mode &&
17763-
var->init_kind == (an_init_kind)initk_none &&
17763+
var->init_kind == initk_none &&
1776417764
var->source_corresp.is_class_member &&
1776517765
!var->is_member_constant &&
1776617766
parent_class_of(var)->variant.class_struct_union.is_nonreal_class &&
@@ -17769,7 +17769,18 @@ be returned for a C mode const variable.
1776917769
in-class initializer as a constant-expression during the prototype
1777017770
instantiation of its class. In a real instantiation, the out-of-class
1777117771
definition must be present. */
17772-
con_val = alloc_constant((a_constant_repr_kind)ck_template_param);
17772+
con_val = alloc_constant(ck_template_param);
17773+
make_template_param_expr_constant(var_rvalue_expr(var), con_val);
17774+
} else if (con_val == NULL && !constexpr_enabled && var->is_nonreal &&
17775+
var->init_kind == initk_none &&
17776+
var->template_info != NULL &&
17777+
var->template_info->template_arg_list != NULL) {
17778+
/* A variable template instance with template-dependent arguments
17779+
(e.g., "vt<T>" in a template), whose value is not known until
17780+
instantiation. A pre-C++11 constant expression (where variable
17781+
templates are an extension) requires a constant here, so treat
17782+
the reference as a template-dependent constant. */
17783+
con_val = alloc_constant(ck_template_param);
1777317784
make_template_param_expr_constant(var_rvalue_expr(var), con_val);
1777417785
} /* if */
1777517786
if (!C_mode() && !constexpr_enabled && strict_ansi_mode &&
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[GH #489]
2+
Template-dependent variable template-ids in C++03 constant expressions
3+
4+
In C++03 GNU and Clang modes, where variable templates are accepted as an
5+
extension, a variable template-id with template-dependent template arguments
6+
was rejected as nonconstant where a constant expression is required (e.g., as
7+
a template argument, an array bound, or an enumerator value). That is now
8+
fixed. Such a reference to a constant variable template is treated as a
9+
value-dependent constant, and its value is checked when the template is
10+
instantiated. For example, with --g++ --c++03:
11+
12+
template <class T> const bool is_fp = false;
13+
template <> const bool is_fp<float> = true;
14+
template <bool B> struct integral_constant {
15+
static const bool value = B;
16+
};
17+
template <class T>
18+
struct is_floating_point
19+
: integral_constant<is_fp<T> > {}; // Previously an error.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fe_only -DTEST_NUMBER=1 --g++ --c++03 --gnu_version=150200 Test_name.c
2+
"Test_name.c", line 13: warning: variable templates are a C++14 feature
3+
template <class T> bool nonconst = false;
4+
^
5+
6+
"Test_name.c", line 14: error: expression must have a constant value
7+
template <class T> struct Z : integral_constant<nonconst<T> > {};
8+
^
9+
detected during instantiation of class "Z<T> [with T=int]" at line 15
10+
11+
1 error detected in the compilation of "Test_name.c".
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fe_only -DTEST_NUMBER=2 --clang --c++03 --clang_version=220100 Test_name.c
2+
"Test_name.c", line 13: warning: variable templates are a C++14 feature
3+
template <class T> bool nonconst = false;
4+
^
5+
6+
"Test_name.c", line 14: error: expression must have a constant value
7+
template <class T> struct Z : integral_constant<nonconst<T> > {};
8+
^
9+
detected during instantiation of class "Z<T> [with T=int]" at line 15
10+
11+
1 error detected in the compilation of "Test_name.c".
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fe_only -DTEST_NUMBER=3 --g++ --c++11 --gnu_version=150200 Test_name.c
2+
"Test_name.c", line 13: warning: variable templates are a C++14 feature
3+
template <class T> bool nonconst = false;
4+
^
5+
6+
"Test_name.c", line 14: error: expression must have a constant value
7+
template <class T> struct Z : integral_constant<nonconst<T> > {};
8+
^
9+
"Test_name.c", line 14: note: the value of variable "nonconst<int>" (declared at line 13) cannot be used as a constant
10+
template <class T> struct Z : integral_constant<nonconst<T> > {};
11+
^
12+
detected during instantiation of class "Z<T> [with T=int]" at line 15
13+
14+
1 error detected in the compilation of "Test_name.c".
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fe_only -DTEST_NUMBER=1 --g++ --c++03 --gnu_version=150200 Test_name.c
2+
"Test_name.c", line 13: warning: variable templates are a C++14 feature
3+
template <class T> inline const bool is_fp_impl = false;
4+
^
5+
6+
"Test_name.c", line 14: warning: inline variables are a C++17 feature
7+
template <> inline const bool is_fp_impl<float> = true;
8+
^
9+
10+
"Test_name.c", line 25: warning: variable templates are a C++14 feature
11+
template <int N> const bool positive = N > 0;
12+
^
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fe_only -DTEST_NUMBER=2 --clang --c++03 --clang_version=220100 Test_name.c
2+
"Test_name.c", line 13: warning: variable templates are a C++14 feature
3+
template <class T> inline const bool is_fp_impl = false;
4+
^
5+
6+
"Test_name.c", line 14: warning: inline variables are a C++17 feature
7+
template <> inline const bool is_fp_impl<float> = true;
8+
^
9+
10+
"Test_name.c", line 25: warning: variable templates are a C++14 feature
11+
template <int N> const bool positive = N > 0;
12+
^
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fe_only -DTEST_NUMBER=3 --g++ --c++11 --gnu_version=150200 Test_name.c
2+
"Test_name.c", line 13: warning: variable templates are a C++14 feature
3+
template <class T> inline const bool is_fp_impl = false;
4+
^
5+
6+
"Test_name.c", line 14: warning: inline variables are a C++17 feature
7+
template <> inline const bool is_fp_impl<float> = true;
8+
^
9+
10+
"Test_name.c", line 25: warning: variable templates are a C++14 feature
11+
template <int N> const bool positive = N > 0;
12+
^
13+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//type:fn
2+
//options:--g++ --c++03 --gnu_version=150200:--clang --c++03 --clang_version=220100:--g++ --c++11 --gnu_version=150200
3+
//remark:Non-constant variable template-id rejected in a constant expression
4+
5+
// A variable template that is not const is still not usable where a constant
6+
// expression is required. The error is issued when the template is
7+
// instantiated.
8+
9+
template <bool B> struct integral_constant {
10+
static const bool value = B;
11+
};
12+
13+
template <class T> bool nonconst = false;
14+
template <class T> struct Z : integral_constant<nonconst<T> > {};
15+
Z<int> z;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//type:fp
2+
//options:--g++ --c++03 --gnu_version=150200:--clang --c++03 --clang_version=220100:--g++ --c++11 --gnu_version=150200
3+
//remark:Template-dependent variable template-ids in C++03 constant expressions
4+
5+
// In C++03 GNU and Clang modes (where variable templates are an extension),
6+
// a variable template-id with template-dependent arguments was rejected as
7+
// nonconstant where a constant expression is required.
8+
9+
template <bool B> struct integral_constant {
10+
static const bool value = B;
11+
};
12+
13+
template <class T> inline const bool is_fp_impl = false;
14+
template <> inline const bool is_fp_impl<float> = true;
15+
template <> inline const bool is_fp_impl<double> = true;
16+
template <> inline const bool is_fp_impl<long double> = true;
17+
18+
template <class T>
19+
struct is_floating_point : integral_constant<is_fp_impl<T> > {};
20+
21+
int a1[is_floating_point<float>::value ? 1 : -1];
22+
int a2[is_floating_point<int>::value ? -1 : 1];
23+
24+
// Value-dependent template argument; other constant-expression contexts.
25+
template <int N> const bool positive = N > 0;
26+
template <int N> struct X : integral_constant<positive<N> > {
27+
int arr[positive<N> ? 1 : 2];
28+
enum { e = positive<N> };
29+
};
30+
int b1[X<1>::value ? 1 : -1];
31+
int b2[sizeof(X<1>().arr) == sizeof(int) ? 1 : -1];
32+
int b3[X<0>::e == 0 ? 1 : -1];
33+
34+
// Member variable template.
35+
struct S {
36+
template <class T> static const bool v = false;
37+
};
38+
template <> const bool S::v<float> = true;
39+
template <class T> struct Y : integral_constant<S::v<T> > {};
40+
int c1[Y<float>::value ? 1 : -1];
41+
int c2[Y<int>::value ? -1 : 1];

0 commit comments

Comments
 (0)