Clang Project

clang_source_code/test/CXX/expr/expr.const/p6.cpp
1// RUN: %clang_cc1 -std=c++17 -verify %s
2
3template<typename T> int not_constexpr() { return T::error; }
4template<typename T> constexpr int is_constexpr() { return T::error; } // expected-error {{'::'}}
5
6template<typename T> int not_constexpr_var = T::error;
7template<typename T> constexpr int is_constexpr_var = T::error; // expected-error {{'::'}}
8template<typename T> const int is_const_var = T::error; // expected-error {{'::'}}
9template<typename T> const volatile int is_const_volatile_var = T::error;
10template<typename T> T is_dependent_var = T::error; // expected-error {{'::'}}
11template<typename T> int &is_reference_var = T::error; // expected-error {{'::'}}
12template<typename T> float is_float_var = T::error;
13
14void test() {
15  // Do not instantiate functions referenced in unevaluated operands...
16  (void)sizeof(not_constexpr<long>());
17  (void)sizeof(is_constexpr<long>());
18  (void)sizeof(not_constexpr_var<long>);
19  (void)sizeof(is_constexpr_var<long>);
20  (void)sizeof(is_const_var<long>);
21  (void)sizeof(is_const_volatile_var<long>);
22  (void)sizeof(is_dependent_var<long>);
23  (void)sizeof(is_dependent_var<const long>);
24  (void)sizeof(is_reference_var<long>);
25  (void)sizeof(is_float_var<long>);
26
27  // ... but do if they are potentially constant evaluated, and refer to
28  // constexpr functions or to variables usable in constant expressions.
29  (void)sizeof(int{not_constexpr<int>()});
30  (void)sizeof(int{is_constexpr<int>()}); // expected-note {{instantiation of}}
31  (void)sizeof(int{not_constexpr_var<int>});
32  (void)sizeof(int{is_constexpr_var<int>}); // expected-note {{instantiation of}}
33  (void)sizeof(int{is_const_var<int>}); // expected-note {{instantiation of}}
34  (void)sizeof(int{is_const_volatile_var<int>});
35  (void)sizeof(int{is_dependent_var<int>});
36  (void)sizeof(int{is_dependent_var<const int>}); // expected-note {{instantiation of}}
37  (void)sizeof(int{is_reference_var<int>}); // expected-note {{instantiation of}}
38  (void)sizeof(int{is_float_var<int>}); // expected-error {{cannot be narrowed}} expected-note {{cast}}
39}
40