blob: 079870947db09bb39e508e0c2d29d89566a9d6bf [file] [log] [blame]
David Majnemerc10b8382015-10-08 06:31:22 +00001// RUN: %clang_cc1 -triple %itanium_abi_triple -fsyntax-only -std=c++11 -verify %s
Richard Smith3c7ad4e2012-02-08 06:41:34 +00002
3// If an expression of literal class type is used in a context where an integral
4// constant expression is required, then that class type shall have a single
5// non-explicit conversion function to an integral or unscoped enumeration type
6namespace std_example {
7
8struct A {
9 constexpr A(int i) : val(i) { }
Richard Smith034185c2013-04-21 01:08:50 +000010 constexpr operator int() const { return val; }
11 constexpr operator long() const { return 43; }
Richard Smith3c7ad4e2012-02-08 06:41:34 +000012private:
13 int val;
14};
15template<int> struct X { };
16constexpr A a = 42;
17X<a> x; // ok, unique conversion to int
18int ary[a]; // expected-error {{size of array has non-integer type 'const std_example::A'}}
19
20}
21
22struct OK {
23 constexpr OK() {}
Richard Smith034185c2013-04-21 01:08:50 +000024 constexpr operator int() const { return 8; }
Richard Smith3c7ad4e2012-02-08 06:41:34 +000025} constexpr ok;
26extern struct Incomplete incomplete; // expected-note 4{{forward decl}}
27struct Explicit {
28 constexpr Explicit() {}
Richard Smith034185c2013-04-21 01:08:50 +000029 constexpr explicit operator int() const { return 4; } // expected-note 4{{here}}
Richard Smith3c7ad4e2012-02-08 06:41:34 +000030} constexpr expl;
31struct Ambiguous {
32 constexpr Ambiguous() {}
Richard Smith034185c2013-04-21 01:08:50 +000033 constexpr operator int() const { return 2; } // expected-note 4{{here}}
34 constexpr operator long() const { return 1; } // expected-note 4{{here}}
Richard Smith3c7ad4e2012-02-08 06:41:34 +000035} constexpr ambig;
36
37constexpr int test_ok = ok; // ok
38constexpr int test_explicit(expl); // ok
39constexpr int test_ambiguous = ambig; // ok
40
41static_assert(test_ok == 8, "");
42static_assert(test_explicit == 4, "");
43static_assert(test_ambiguous == 2, "");
44
45// [expr.new]p6: Every constant-expression in a noptr-new-declarator shall be
46// an integral constant expression
47auto new1 = new int[1][ok];
48auto new2 = new int[1][incomplete]; // expected-error {{incomplete}}
49auto new3 = new int[1][expl]; // expected-error {{explicit conversion}}
50auto new4 = new int[1][ambig]; // expected-error {{ambiguous conversion}}
51
52// [dcl.enum]p5: If the underlying type is not fixed [...] the initializing
53// value [...] shall be an integral constant expression.
54enum NotFixed {
55 enum1 = ok,
56 enum2 = incomplete, // expected-error {{incomplete}}
57 enum3 = expl, // expected-error {{explicit conversion}}
58 enum4 = ambig // expected-error {{ambiguous conversion}}
59};
60
61// [dcl.align]p2: When the alignment-specifier is of the form
62// alignas(assignment-expression), the assignment-expression shall be an
63// integral constant expression
Michael Han64536a62012-11-06 19:34:54 +000064alignas(ok) int alignas1;
65alignas(incomplete) int alignas2; // expected-error {{incomplete}}
66alignas(expl) int alignas3; // expected-error {{explicit conversion}}
67alignas(ambig) int alignas4; // expected-error {{ambiguous conversion}}
Richard Smith3c7ad4e2012-02-08 06:41:34 +000068
69// [dcl.array]p1: If the constant-expression is present, it shall be an integral
70// constant expression
71// FIXME: The VLA recovery results in us giving diagnostics which aren't great
72// here.
73int array1[ok];
74int array2[incomplete]; // expected-error {{non-integer type}}
75int array3[expl]; // expected-error {{non-integer type}}
76int array4[ambig]; // expected-error {{non-integer type}}
77
78// [class.bit]p1: The constasnt-expression shall be an integral constant
79// expression
80struct Bitfields {
81 int bitfield1 : ok;
82 int bitfield2 : incomplete; // expected-error {{incomplete}}
83 int bitfield3 : expl; // expected-error {{explicit conversion}}
84 int bitfield4 : ambig; // expected-error {{ambiguous conversion}}
85};