p5-0x.cpp 3.2 KB

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