dr14xx.cpp 14.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
// RUN: %clang_cc1 -std=c++98 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
// RUN: %clang_cc1 -std=c++11 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
// RUN: %clang_cc1 -std=c++14 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
// RUN: %clang_cc1 -std=c++17 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
// RUN: %clang_cc1 -std=c++2a %s -verify -fexceptions -fcxx-exceptions -pedantic-errors

namespace dr1423 { // dr1423: 11
#if __cplusplus >= 201103L
  bool b1 = nullptr; // expected-error {{cannot initialize}}
  bool b2(nullptr); // expected-warning {{implicit conversion of nullptr constant to 'bool'}}
  bool b3 = {nullptr}; // expected-error {{cannot initialize}}
  bool b4{nullptr}; // expected-warning {{implicit conversion of nullptr constant to 'bool'}}
#endif
}

namespace dr1443 { // dr1443: yes
struct A {
  int i;
  A() { void foo(int=i); } // expected-error {{default argument references 'this'}}
};
}

// dr1425: na abi

namespace dr1460 { // dr1460: 3.5
#if __cplusplus >= 201103L
  namespace DRExample {
    union A {
      union {}; // expected-error {{does not declare anything}}
      union {}; // expected-error {{does not declare anything}}
      constexpr A() {}
    };
    constexpr A a = A();

    union B {
      union {}; // expected-error {{does not declare anything}}
      union {}; // expected-error {{does not declare anything}}
      constexpr B() = default;
    };
    constexpr B b = B();

    union C {
      union {}; // expected-error {{does not declare anything}}
      union {}; // expected-error {{does not declare anything}}
    };
    constexpr C c = C();
#if __cplusplus > 201103L
    constexpr void f() { C c; }
    static_assert((f(), true), "");
#endif
  }

  union A {};
  union B { int n; }; // expected-note 0+{{here}}
  union C { int n = 0; };
  struct D { union {}; }; // expected-error {{does not declare anything}}
  struct E { union { int n; }; }; // expected-note 0+{{here}}
  struct F { union { int n = 0; }; };

  struct X {
    friend constexpr A::A() noexcept;
    friend constexpr B::B() noexcept;
#if __cplusplus <= 201703L
    // expected-error@-2 {{follows non-constexpr declaration}}
#endif
    friend constexpr C::C() noexcept;
    friend constexpr D::D() noexcept;
    friend constexpr E::E() noexcept;
#if __cplusplus <= 201703L
    // expected-error@-2 {{follows non-constexpr declaration}}
#endif
    friend constexpr F::F() noexcept;
  };

  // These are OK, because value-initialization doesn't actually invoke the
  // constructor.
  constexpr A a = A();
  constexpr B b = B();
  constexpr C c = C();
  constexpr D d = D();
  constexpr E e = E();
  constexpr F f = F();

  namespace Defaulted {
    union A { constexpr A() = default; };
    union B { int n; constexpr B() = default; };
#if __cplusplus <= 201703L
    // expected-error@-2 {{not constexpr}}
#endif
    union C { int n = 0; constexpr C() = default; };
    struct D { union {}; constexpr D() = default; }; // expected-error {{does not declare anything}}
    struct E { union { int n; }; constexpr E() = default; };
#if __cplusplus <= 201703L
    // expected-error@-2 {{not constexpr}}
#endif
    struct F { union { int n = 0; }; constexpr F() = default; };

    struct G { union { int n = 0; }; union { int m; }; constexpr G() = default; };
#if __cplusplus <= 201703L
    // expected-error@-2 {{not constexpr}}
#endif
    struct H {
      union {
        int n = 0;
      };
      union { // expected-note 0-2{{member not initialized}}
        int m;
      };
      constexpr H() {}
#if __cplusplus <= 201703L
      // expected-error@-2 {{initialize all members}}
#endif
      constexpr H(bool) : m(1) {}
      constexpr H(char) : n(1) {}
#if __cplusplus <= 201703L
      // expected-error@-2 {{initialize all members}}
#endif
      constexpr H(double) : m(1), n(1) {}
    };
  }

#if __cplusplus > 201103L
  template<typename T> constexpr bool check() {
    T t;
#if __cplusplus <= 201703L
    // expected-note-re@-2 2{{non-constexpr constructor '{{[BE]}}'}}
#endif
    return true;
  }
  static_assert(check<A>(), "");
  static_assert(check<B>(), "");
#if __cplusplus <= 201703L
  // expected-error@-2 {{constant}} expected-note@-2 {{in call}}
#endif
  static_assert(check<C>(), "");
  static_assert(check<D>(), "");
  static_assert(check<E>(), "");
#if __cplusplus <= 201703L
  // expected-error@-2 {{constant}} expected-note@-2 {{in call}}
#endif
  static_assert(check<F>(), "");
#endif

  union G {
    int a = 0; // expected-note {{previous initialization is here}}
    int b = 0; // expected-error {{initializing multiple members of union}}
  };
  union H {
    union {
      int a = 0; // expected-note {{previous initialization is here}}
    };
    union {
      int b = 0; // expected-error {{initializing multiple members of union}}
    };
  };
  struct I {
    union {
      int a = 0; // expected-note {{previous initialization is here}}
      int b = 0; // expected-error {{initializing multiple members of union}}
    };
  };
  struct J {
    union { int a = 0; };
    union { int b = 0; };
  };

  namespace Overriding {
    struct A {
      int a = 1, b, c = 3;
      constexpr A() : b(2) {}
    };
    static_assert(A().a == 1 && A().b == 2 && A().c == 3, "");

    union B {
      int a, b = 2, c;
      constexpr B() : a(1) {}
      constexpr B(char) : b(4) {}
      constexpr B(int) : c(3) {}
      constexpr B(const char*) {}
    };
    static_assert(B().a == 1, "");
    static_assert(B().b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
    static_assert(B('x').a == 0, ""); // expected-error {{constant}} expected-note {{read of}}
    static_assert(B('x').b == 4, "");
    static_assert(B(123).b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
    static_assert(B(123).c == 3, "");
    static_assert(B("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}}
    static_assert(B("").b == 2, "");
    static_assert(B("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}}

    struct C {
      union { int a, b = 2, c; };
      union { int d, e = 5, f; };
      constexpr C() : a(1) {}
      constexpr C(char) : c(3) {}
      constexpr C(int) : d(4) {}
      constexpr C(float) : f(6) {}
      constexpr C(const char*) {}
    };

    static_assert(C().a == 1, "");
    static_assert(C().b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
    static_assert(C().d == 4, ""); // expected-error {{constant}} expected-note {{read of}}
    static_assert(C().e == 5, "");

    static_assert(C('x').b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
    static_assert(C('x').c == 3, "");
    static_assert(C('x').d == 4, ""); // expected-error {{constant}} expected-note {{read of}}
    static_assert(C('x').e == 5, "");

    static_assert(C(1).b == 2, "");
    static_assert(C(1).c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
    static_assert(C(1).d == 4, "");
    static_assert(C(1).e == 5, ""); // expected-error {{constant}} expected-note {{read of}}

    static_assert(C(1.f).b == 2, "");
    static_assert(C(1.f).c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
    static_assert(C(1.f).e == 5, ""); // expected-error {{constant}} expected-note {{read of}}
    static_assert(C(1.f).f == 6, "");

    static_assert(C("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}}
    static_assert(C("").b == 2, "");
    static_assert(C("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
    static_assert(C("").d == 4, ""); // expected-error {{constant}} expected-note {{read of}}
    static_assert(C("").e == 5, "");
    static_assert(C("").f == 6, ""); // expected-error {{constant}} expected-note {{read of}}

    struct D;
    extern const D d;
    struct D {
      int a;
      union {
        int b = const_cast<D&>(d).a = 1; // not evaluated
        int c;
      };
      constexpr D() : a(0), c(0) {}
    };
    constexpr D d {};
    static_assert(d.a == 0, "");
  }
#endif
}

#if __cplusplus >= 201103L
namespace std {
  typedef decltype(sizeof(int)) size_t;

  // libc++'s implementation
  template <class _E>
  class initializer_list
  {
    const _E* __begin_;
    size_t    __size_;

    initializer_list(const _E* __b, size_t __s)
    : __begin_(__b), __size_(__s) {}

  public:
    typedef _E        value_type;
    typedef const _E& reference;
    typedef const _E& const_reference;
    typedef size_t    size_type;

    typedef const _E* iterator;
    typedef const _E* const_iterator;

    initializer_list() : __begin_(nullptr), __size_(0) {}

    size_t    size()  const {return __size_;}
    const _E* begin() const {return __begin_;}
    const _E* end()   const {return __begin_ + __size_;}
  };
} // std

namespace dr1467 {  // dr1467: 3.7 c++11
  // List-initialization of aggregate from same-type object

  namespace basic0 {
    struct S {
      int i = 42;
    };

    S a;
    S b(a);
    S c{a};

    struct SS : public S { } x;
    S y(x);
    S z{x};
  } // basic0

  namespace basic1 {
    struct S {
      int i{42};
    };

    S a;
    S b(a);
    S c{a};

    struct SS : public S { } x;
    S y(x);
    S z{x};
  } // basic1

  namespace basic2 {
    struct S {
      int i = {42};
    };

    S a;
    S b(a);
    S c{a};

    struct SS : public S { } x;
    S y(x);
    S z{x};
  } // basic2

  namespace dr_example {
    struct OK {
      OK() = default;
      OK(const OK&) = default;
      OK(int) { }
    };

    OK ok;
    OK ok2{ok};

    struct X {
      X() = default;
      X(const X&) = default;
    };

    X x;
    X x2{x};

    void f1(int);                                  // expected-note {{candidate function}}
    void f1(std::initializer_list<long>) = delete; // expected-note {{candidate function has been explicitly deleted}}
    void g1() { f1({42}); }                        // expected-error {{call to deleted function 'f1'}}

    template <class T, class U>
    struct Pair {
      Pair(T, U);
    };
    struct String {
      String(const char *);
    };

    void f2(Pair<const char *, const char *>);       // expected-note {{candidate function}}
    void f2(std::initializer_list<String>) = delete; // expected-note {{candidate function has been explicitly deleted}}
    void g2() { f2({"foo", "bar"}); }                // expected-error {{call to deleted function 'f2'}}
  } // dr_example

  namespace nonaggregate {
    struct NonAggregate {
      NonAggregate() {}
    };

    struct WantsIt {
      WantsIt(NonAggregate);
    };

    void f(NonAggregate);
    void f(WantsIt);

    void test1() {
      NonAggregate n;
      f({n});
    }

    void test2() {
      NonAggregate x;
      NonAggregate y{x};
      NonAggregate z{{x}};
    }
  } // nonaggregate

  namespace SelfInitIsNotListInit {
    struct S {
      S();
      explicit S(S &);
      S(const S &);
    };
    S s1;
    S s2 = {s1}; // ok, not list-initialization so we pick the non-explicit constructor
  }

  struct NestedInit { int a, b, c; };
  NestedInit ni[1] = {{NestedInit{1, 2, 3}}};

  namespace NestedInit2 {
    struct Pair { int a, b; };
    struct TwoPairs { TwoPairs(Pair, Pair); };
    struct Value { Value(Pair); Value(TwoPairs); };
    void f() { Value{{{1,2},{3,4}}}; }
  }
  namespace NonAmbiguous {
  // The original implementation made this case ambigious due to the special
  // handling of one element initialization lists.
  void f(int(&&)[1]);
  void f(unsigned(&&)[1]);

  void g(unsigned i) {
    f({i});
  }
  } // namespace NonAmbiguous

#if __cplusplus >= 201103L
  namespace StringLiterals {
  // When the array size is 4 the call will attempt to bind an lvalue to an
  // rvalue and fail. Therefore #2 will be called. (rsmith will bring this
  // issue to CWG)
  void f(const char(&&)[4]);              // expected-note 5 {{no known conversion}}
  void f(const char(&&)[5]) = delete;     // expected-note 2 {{candidate function has been explicitly deleted}} expected-note 3 {{no known conversion}}
  void f(const wchar_t(&&)[4]);           // expected-note 5 {{no known conversion}}
  void f(const wchar_t(&&)[5]) = delete;  // expected-note {{candidate function has been explicitly deleted}} expected-note 4 {{no known conversion}}
#if __cplusplus >= 202002L
  void f2(const char8_t(&&)[4]);          // expected-note {{no known conversion}}
  void f2(const char8_t(&&)[5]) = delete; // expected-note {{candidate function has been explicitly deleted}}
#endif
  void f(const char16_t(&&)[4]);          // expected-note 5 {{no known conversion}}
  void f(const char16_t(&&)[5]) = delete; // expected-note {{candidate function has been explicitly deleted}} expected-note 4 {{no known conversion}}
  void f(const char32_t(&&)[4]);          // expected-note 5 {{no known conversion}}
  void f(const char32_t(&&)[5]) = delete; // expected-note {{candidate function has been explicitly deleted}} expected-note 4 {{no known conversion}}
  void g() {
    f({"abc"});       // expected-error {{call to deleted function 'f'}}
    f({((("abc")))}); // expected-error {{call to deleted function 'f'}}
    f({L"abc"});      // expected-error {{call to deleted function 'f'}}
#if __cplusplus >= 202002L
    f2({u8"abc"});    // expected-error {{call to deleted function 'f2'}}
#endif
    f({uR"(abc)"});   // expected-error {{call to deleted function 'f'}}
    f({(UR"(abc)")}); // expected-error {{call to deleted function 'f'}}
  }
  } // namespace StringLiterals
#endif
} // dr1467

namespace dr1490 {  // dr1490: 3.7 c++11
  // List-initialization from a string literal

  char s[4]{"abc"};                   // Ok
  std::initializer_list<char>{"abc"}; // expected-error {{expected unqualified-id}}}
} // dr190

namespace dr1495 { // dr1495: 4
  // Deduction succeeds in both directions.
  template<typename T, typename U> struct A {}; // expected-note {{template is declared here}}
  template<typename T, typename U> struct A<U, T> {}; // expected-error {{class template partial specialization is not more specialized}}

  // Primary template is more specialized.
  template<typename, typename...> struct B {}; // expected-note {{template is declared here}}
  template<typename ...Ts> struct B<Ts...> {}; // expected-error {{not more specialized}}

  // Deduction fails in both directions.
  template<int, typename, typename ...> struct C {}; // expected-note {{template is declared here}}
  template<typename ...Ts> struct C<0, Ts...> {}; // expected-error {{not more specialized}}

#if __cplusplus >= 201402L
  // Deduction succeeds in both directions.
  template<typename T, typename U> int a; // expected-note {{template is declared here}}
  template<typename T, typename U> int a<U, T>; // expected-error {{variable template partial specialization is not more specialized}}

  // Primary template is more specialized.
  template<typename, typename...> int b; // expected-note {{template is declared here}}
  template<typename ...Ts> int b<Ts...>; // expected-error {{not more specialized}}

  // Deduction fails in both directions.
  template<int, typename, typename ...> int c; // expected-note {{template is declared here}}
  template<typename ...Ts> int c<0, Ts...>; // expected-error {{not more specialized}}
#endif
}
#endif