brintos

brintos / llvm-project-archived public Read only

0
0
Text · 19.8 KiB · c106de1 Raw
750 lines · cpp
1// RUN: %clang_cc1 -std=c++17 -fsyntax-only %s -verify=expected,pre20 -Wno-c++2a-extensions2// RUN: %clang_cc1 -std=c++2a -fsyntax-only %s -verify=expected,post203 4template <bool b, auto val> struct enable_ifv {};5 6template <auto val> struct enable_ifv<true, val> {7  static constexpr auto value = val;8};9 10template <typename T1, typename T2> struct is_same {11  static constexpr bool value = false;12};13 14template <typename T> struct is_same<T, T> {15  static constexpr bool value = true;16};17 18namespace special_cases19{20 21template<int a>22struct A {23// pre20-note@-1+ {{candidate constructor}}24  explicit(1 << a)25// expected-note@-1 {{negative shift count -1}}26// expected-error@-2 {{explicit specifier argument is not a constant expression}}27  A(int);28};29 30A<-1> a(0);31// pre20-error@-1 {{no matching constructor}}32// post20-error@-2 {{excess elements in struct initializer}}33// expected-note@-3 {{in instantiation of template class}}34 35template<int a>36struct B {37  explicit(b)38  // expected-error@-1 {{use of undeclared identifier}}39  B(int);40};41 42template<int a>43struct B1 {44  explicit(a +)45  // expected-error@-1 {{expected expression}}46  B1(int);47};48 49struct B2 {50  explicit(false) explicit51  B2(int);52  // expected-error@-2 {{duplicate 'explicit' declaration specifier}}53};54 55template<int a>56  struct C {57  // expected-note@-1 {{candidate constructor}} expected-note@-1 {{candidate constructor}}58  // expected-note@-2 {{candidate constructor}} expected-note@-2 {{candidate constructor}}59  explicit(a == 0)60C(int), // expected-note 2{{not a candidate}}61C(double); // expected-note 2{{not a candidate}}62};63 64C<0> c0 = 0.0; // expected-error {{no viable conversion}}65C<0> c1 = 0; // expected-error {{no viable conversion}}66C<1> c2 = 0.0;67C<1> c3 = 0;68 69explicit(false) void f(int);// expected-error {{'explicit' can only be specified inside the class definition}}70 71struct D {72  explicit(false) void f(int);// expected-error {{'explicit' can only be applied to a constructor or conversion function}}73};74 75template <typename T> struct E {76  // expected-note@-1+ {{candidate constructor}}77  explicit((T{}, false))78  // expected-error@-1 {{cannot create object of function type 'void ()'}}79  E(int);80};81 82E<void ()> e = 1;83// expected-error@-1 {{no viable conversion}}84// expected-note@-2 {{in instantiation of}}85 86}87 88namespace trailing_object {89 90template<bool b>91struct B {92  explicit(b) B(int) {}93};94 95template<bool b>96struct A : B<b> {97  explicit(b) A(int) : B<b>(0) {}98};99 100A<true> a(0);101 102}103 104namespace constructor1 {105 106template<bool b>107  struct A {108    // expected-note@-1+ {{candidate constructor}}109    // expected-note@-2+ {{candidate function}}110    explicit(b) A(int, int = 0); // expected-note {{not a candidate}}111  // expected-note@-1+ {{explicit constructor declared here}}112};113 114template<bool b>115A<b>::A(int, int) {}116 117void f()118{119A<true> a0 = 0; // expected-error {{no viable conversion}}120A<true> a1( 0);121A<true> && a2 = 0;// expected-error {{could not bind}}122A<true> && a3( 0);// expected-error {{could not bind}}123A<true> a4{ 0};124A<true> && a5 = { 0};// expected-error {{chosen constructor is explicit}}125A<true> && a6{ 0};126A<true> a7 = { 0}; // expected-error {{chosen constructor is explicit in copy-initialization}}127 128a0 = 0; // expected-error {{no viable overloaded '='}}129a1 = { 0}; // expected-error {{no viable overloaded '='}}130a2 = A<true>( 0);131a3 = A<true>{ 0};132 133A<false> c0 =  ((short)0);134A<false> c1( ((short)0));135A<false> && c2 =  ((short)0);136A<false> && c3( ((short)0));137A<false> c4{ ((short)0)};138A<false> && c5 = { ((short)0)};139A<false> && c6{ ((short)0)};140 141A<true> d1( 0, 0);142A<true> d2{ 0, 0};143A<true> d3 = { 0, 0}; // expected-error {{chosen constructor is explicit in copy-initialization}}144 145d1 = { 0, 0}; // expected-error {{no viable overloaded '='}}146d2 = A<true>( 0, 0);147d3 = A<true>{ 0, 0};148}149}150 151namespace constructor2 {152 153template<bool a, typename T1>154struct A {155  // expected-note@-1 {{candidate constructor}} expected-note@-1 {{candidate constructor}}156  // expected-note@-2 {{candidate constructor}} expected-note@-2 {{candidate constructor}}157  template<typename T2>158  explicit(a ^ is_same<T1, T2>::value)159  A(T2) {}160  // expected-note@-1+ {{explicit constructor declared here}}161  // expected-note@-2+ {{not a candidate}}162};163 164A<true, int> a0 = 0.0; // expected-error {{no viable conversion}}165A<true, int> a1( 0.0);166A<true, int> && a2 = 0.0;// expected-error {{could not bind}}167A<true, int> && a3( 0.0);// expected-error {{could not bind}}168A<true, int> a4{ 0.0};169A<true, int> && a5 = { 0.0};// expected-error {{chosen constructor is explicit}}170A<true, int> && a6{ 0.0};171A<true, int> a7 = { 0.0}; // expected-error {{chosen constructor is explicit in copy-initialization}}172 173A<true, int> b0 = 0;174A<true, int> b1( 0);175A<true, int> && b2 = 0;176A<true, int> && b3( 0);177A<true, int> b4{ 0};178A<true, int> && b5 = { 0};179A<true, int> && b6{ 0};180A<true, int> b7 = { 0};181 182A<true, double> c0 = 0; // expected-error {{no viable conversion}}183A<true, double> c1( 0);184A<true, double> && c2 = 0;// expected-error {{could not bind}}185A<true, double> && c3( 0);// expected-error {{could not bind}}186A<true, double> c4{ 0};187A<true, double> && c5 = { 0};// expected-error {{chosen constructor is explicit}}188A<true, double> && c6{ 0};189A<true, double> c7 = { 0}; // expected-error {{chosen constructor is explicit in copy-initialization}}190 191}192 193namespace constructor_sfinae {194 195template<bool a>196struct A {197  // expected-note@-1+ {{candidate constructor}}198  template<typename T>199  explicit(enable_ifv<is_same<int, T>::value, a>::value)200  A(T) {}201  // expected-note@-1+ {{substitution failure}}202  // expected-note@-2 {{not a candidate}}203  // expected-note@-3 {{explicit constructor declared here}}204  template<typename T, bool c = true>205  explicit(enable_ifv<is_same<bool, T>::value, a>::value)206  A(T) {}207  // expected-note@-1+ {{substitution failure}}208  // expected-note@-2 {{not a candidate}}209  // expected-note@-3 {{explicit constructor declared here}}210};211 212A<true> a0 = 0.0; // expected-error {{no viable conversion}}213A<true> a1( 0.0); // expected-error {{no matching constructor}}214A<true> a4{ 0.0}; // expected-error {{no matching constructor}}215A<true> a7 = { 0.0}; // expected-error {{no matching constructor}}216 217A<true> b0 = 0; // expected-error {{no viable conversion}}218A<true> b1( 0);219A<true> b4{ 0};220A<true> b7 = { 0}; // expected-error {{chosen constructor is explicit}}221 222A<false> c0 = 0;223A<false> c1( 0);224A<false> c4{ 0};225A<false> c7 = { 0};226 227A<true> d0 = true; // expected-error {{no viable conversion}}228A<true> d1( true);229A<true> d4{ true};230A<true> d7 = { true}; // expected-error {{chosen constructor is explicit}}231 232}233 234namespace conversion {235 236template<bool a>237struct A {238  explicit(a) operator int (); // expected-note+ {{not a candidate}}239};240 241template<bool a>242A<a>::operator int() {243  return 0;244}245 246A<true> A_true;247A<false> A_false;248 249int ai0 = A<true>(); // expected-error {{no viable conversion}}250const int& ai1 = A<true>(); // expected-error {{no viable conversion}}251int&& ai3 = A<true>(); // expected-error {{no viable conversion}}252int ai4 = A_true; // expected-error {{no viable conversion}}253const int& ai5 = A_true; // expected-error {{no viable conversion}}254 255int ai01 = {A<true>()}; // expected-error {{no viable conversion}}256const int& ai11 = {A<true>()}; // expected-error {{no viable conversion}}257int&& ai31 = {A<true>()}; // expected-error {{no viable conversion}}258int ai41 = {A_true}; // expected-error {{no viable conversion}}259const int& ai51 = {A_true}; // expected-error {{no viable conversion}}260 261int ae0(A<true>());262const int& ae1(A<true>());263int&& ae3(A<true>());264int ae4(A_true);265const int& ae5(A_true);266 267int bi0 = A<false>();268const int& bi1 = A<false>();269int&& bi3 = A<false>();270int bi4 = A_false;271const int& bi5 = A_false;272 273int bi01 = {A<false>()};274const int& bi11 = {A<false>()};275int&& bi31 = {A<false>()};276int bi41 = {A_false};277const int& bi51 = {A_false};278 279int be0(A<true>());280const int& be1(A<true>());281int&& be3(A<true>());282int be4(A_true);283const int& be5(A_true);284 285}286 287namespace conversion2 {288 289struct B {};290// expected-note@-1+ {{candidate constructor}}291template<bool a>292struct A {293  template<typename T2>294  explicit(enable_ifv<is_same<B, T2>::value, a>::value)295  operator T2() { return T2(); };296  // expected-note@-1+ {{substitution failure}}297  // expected-note@-2+ {{not a candidate}}298};299 300A<false> A_false;301A<true> A_true;302 303int ai0 = A<true>(); // expected-error {{no viable conversion}}304const int& ai1 = A<true>(); // expected-error {{no viable conversion}}305int&& ai3 = A<true>(); // expected-error {{no viable conversion}}306int ai4 = A_false; // expected-error {{no viable conversion}}307const int& ai5 = A_false; // expected-error {{no viable conversion}}308 309int ae0{A<true>()};  // expected-error {{no viable conversion}}310const int& ae1{A<true>()};  // expected-error {{no viable conversion}}311int&& ae3{A<true>()};  // expected-error {{no viable conversion}}312int ae4{A_true};  // expected-error {{no viable conversion}}313const int& ae5{A_true};  // expected-error {{no viable conversion}}314 315int ap0((A<true>()));  // expected-error {{no viable conversion}}316const int& ap1((A<true>()));  // expected-error {{no viable conversion}}317int&& ap3((A<true>()));  // expected-error {{no viable conversion}}318int ap4(A_true);  // expected-error {{no viable conversion}}319const int& ap5(A_true);  // expected-error {{no viable conversion}}320 321B b0 = A<true>(); // expected-error {{no viable conversion}}322const B & b1 = A<true>(); // expected-error {{no viable conversion}}323B && b3 = A<true>(); // expected-error {{no viable conversion}}324B b4 = A_true; // expected-error {{no viable conversion}}325const B & b5 = A_true; // expected-error {{no viable conversion}}326 327B be0(A<true>());328const B& be1(A<true>());329B&& be3(A<true>());330B be4(A_true);331const B& be5(A_true);332 333B c0 = A<false>();334const B & c1 = A<false>();335B && c3 = A<false>();336B c4 = A_false;337const B & c5 = A_false;338 339}340 341namespace parameter_pack {342 343template<typename T>344struct A {345  // expected-note@-1+ {{candidate constructor}}346  // expected-note@-2+ {{candidate function}}347  template<typename ... Ts>348  explicit((is_same<T, Ts>::value && ...))349  A(Ts...);350  // expected-note@-1 {{not a candidate}}351  // expected-note@-2 {{explicit constructor}}352};353 354template<typename T>355template<typename ... Ts>356A<T>::A(Ts ...) {}357 358void f() {359 360A<int> a0 = 0; // expected-error {{no viable conversion}}361A<int> a1( 0, 1);362A<int> a2{ 0, 1};363A<int> a3 = { 0, 1}; // expected-error {{chosen constructor is explicit}}364 365a1 = 0; // expected-error {{no viable overloaded '='}}366a2 = { 0, 1}; // expected-error {{no viable overloaded '='}}367 368A<double> b0 = 0;369A<double> b1( 0, 1);370A<double> b2{ 0, 1};371A<double> b3 = { 0, 1};372 373b1 = 0;374b2 = { 0, 1};375 376}377 378}379 380namespace deduction_guide {381 382template<bool b>383struct B {};384 385B<true> b_true;386B<false> b_false;387 388template<typename T>389struct nondeduced390{391using type = T;392};393 394template<typename T1, typename T2, bool b>395struct A {396  // expected-note@-1+ {{candidate function}}397  // expected-note@-2+ {{implicit deduction guide}}398  explicit(false)399  A(typename nondeduced<T1>::type, typename nondeduced<T2>::type, typename nondeduced<B<b>>::type) {}400  // expected-note@-1+ {{candidate template ignored}}401  // expected-note@-2+ {{implicit deduction guide}}402};403 404template<typename T1, typename T2, bool b>405explicit(enable_ifv<is_same<T1, T2>::value, b>::value)406A(T1, T2, B<b>) -> A<T1, T2, b>;407// expected-note@-1+ {{explicit deduction guide declared here}}408// expected-note@-2+ {{candidate template ignored}}409void f() {410 411A a0( 0.0, 1, b_true); // expected-error {{no viable constructor or deduction guide}}412A a1{ 0.0, 1, b_true}; // expected-error {{no viable constructor or deduction guide}}413A a2 = { 0.0, 1, b_true}; // expected-error {{no viable constructor or deduction guide}}414auto a4 = A( 0.0, 1, b_true); // expected-error {{no viable constructor or deduction guide}}415auto a5 = A{ 0.0, 1, b_true}; // expected-error {{no viable constructor or deduction guide}}416 417A b0( 0, 1, b_true);418A b1{ 0, 1, b_true};419A b2 = { 0, 1, b_true}; // expected-error {{explicit deduction guide for copy-list-initialization}}420auto b4 = A( 0, 1, b_true);421auto b5 = A{ 0, 1, b_true};422b0 = { 0, 1, b_false}; // expected-error {{no viable overloaded '='}}423 424A c0( 0, 1, b_false);425A c1{ 0, 1, b_false};426A c2 = { 0, 1, b_false};427auto c4 = A( 0, 1, b_false);428auto c5 = A{ 0, 1, b_false};429c2 = { 0, 1, b_false};430 431}432 433}434 435namespace test8 {436 437template<bool b>438struct A {439  //expected-note@-1+ {{candidate function}}440  template<typename T1, typename T2>441  explicit(b)442  A(T1, T2) {}443  //expected-note@-1 {{explicit constructor declared here}}444};445 446template<typename T1, typename T2>447explicit(!is_same<T1, int>::value)448A(T1, T2) -> A<!is_same<int, T2>::value>;449// expected-note@-1+ {{explicit deduction guide declared here}}450 451template<bool b>452A<b> v();453 454void f() {455 456A a0( 0, 1);457A a1{ 0, 1};458A a2 = { 0, 1};459auto a4 = A( 0, 1);460auto a5 = A{ 0, 1};461auto a6(v<false>());462a6 = { 0, 1};463 464A b0( 0.0, 1);465A b1{ 0.0, 1};466A b2 = { 0.0, 1}; // expected-error {{explicit deduction guide for copy-list-initialization}}467auto b4 = A( 0.0, 1);468auto b5 = A{ 0.0, 1};469 470A c0( 0, 1.0);471A c1{ 0, 1.0};472A c2 = { 0, 1.0}; // expected-error {{chosen constructor is explicit}}473auto c4 = A( 0, 1.0);474auto c5 = A{ 0, 1.0};475auto c6(v<true>());476c0 = { 0, 1.0}; // expected-error {{no viable overloaded '='}}477 478A d0( 0.0, 1.0);479A d1{ 0.0, 1.0};480A d2 = { 0.0, 1.0};  // expected-error {{explicit deduction guide for copy-list-initialization}}481auto d4 = A( 0.0, 1.0);482auto d5 = A{ 0.0, 1.0};483 484}485 486}487 488namespace conversion3 {489 490template<bool b>491struct A {492  explicit(!b) operator int();493  explicit(b) operator bool();494};495 496template<bool b>497A<b>::operator bool() { return false; }498 499struct B {500  void f(int);501  void f(bool);502};503 504void f(A<true> a, B b) {505  b.f(a);506}507 508void f1(A<false> a, B b) {509  b.f(a);510}511 512// Taken from 12.3.2p2513class X { X(); };514class Y { }; // expected-note+ {{candidate constructor (the implicit}}515 516template<bool b>517struct Z {518  explicit(b) operator X() const;519  explicit(b) operator Y() const; // expected-note 2{{not a candidate}}520  explicit(b) operator int() const; // expected-note {{not a candidate}}521};522 523void testExplicit()524{525Z<true> z;526// 13.3.1.4p1 & 8.5p16:527Y y2 = z; // expected-error {{no viable conversion}}528Y y2b(z);529Y y3 = (Y)z;530Y y4 = Y(z);531Y y5 = static_cast<Y>(z);532// 13.3.1.5p1 & 8.5p16:533int i1 = (int)z;534int i2 = int(z);535int i3 = static_cast<int>(z);536int i4(z);537// 13.3.1.6p1 & 8.5.3p5:538const Y& y6 = z; // expected-error {{no viable conversion}}539const int& y7 = z; // expected-error {{no viable conversion}}540const Y& y8(z);541const int& y9(z);542 543// Y is an aggregate, so aggregate-initialization is performed and the544// conversion function is not considered.545const Y y10{z}; // expected-error {{excess elements}}546const Y& y11{z}; // expected-error {{excess elements}} expected-note {{in initialization of temporary}}547const int& y12{z};548 549// X is not an aggregate, so constructors are considered,550// per 13.3.3.1/4 & DR1467.551const X x1{z};552const X& x2{z};553}554 555struct tmp {};556 557template<typename T1>558struct C {559  template<typename T>560  explicit(!is_same<T1, T>::value)561  operator T(); // expected-note+ {{explicit conversion function is not a candidate}}562};563 564using Bool = C<bool>;565using Integral = C<int>;566using Unrelated = C<tmp>;567 568void testBool() {569Bool    b;570Integral n;571Unrelated u;572 573(void) (1 + b); // expected-error {{invalid operands to binary expression}}574(void) (1 + n);575(void) (1 + u); // expected-error {{invalid operands to binary expression}}576 577// 5.3.1p9:578(void) (!b);579(void) (!n);580(void) (!u);581 582// 5.14p1:583(void) (b && true);584(void) (n && true);585(void) (u && true);586 587// 5.15p1:588(void) (b || true);589(void) (n || true);590(void) (u || true);591 592// 5.16p1:593(void) (b ? 0 : 1);594(void) (n ? 0: 1);595(void) (u ? 0: 1);596 597// // 5.19p5:598// // TODO: After constexpr has been implemented599 600// 6.4p4:601if (b) {}602if (n) {}603if (u) {}604 605// 6.4.2p2:606switch (b) {} // expected-error {{statement requires expression of integer type}}607switch (n) {} // expected-error {{statement requires expression of integer type}}608switch (u) {} // expected-error {{statement requires expression of integer type}}609 610// 6.5.1:611while (b) {}612while (n) {}613while (u) {}614 615// 6.5.2p1:616do {} while (b);617do {} while (n);618do {} while (u);619 620// 6.5.3:621for (;b;) {}622for (;n;) {}623for (;u;) {}624 625// 13.3.1.5p1:626bool db1(b);627bool db2(n);628bool db3(u);629int di1(b);630int di2(n);631int di3(n);632const bool &direct_cr1(b);633const bool &direct_cr2(n);634const bool &direct_cr3(n);635const int &direct_cr4(b);636const int &direct_cr5(n);637const int &direct_cr6(n);638bool directList1{b};639bool directList2{n};640bool directList3{n};641int directList4{b};642int directList5{n};643int directList6{n};644const bool &directList_cr1{b};645const bool &directList_cr2{n};646const bool &directList_cr3{n};647const int &directList_cr4{b};648const int &directList_cr5{n};649const int &directList_cr6{n};650bool copy1 = b;651bool copy2 = n;// expected-error {{no viable conversion}}652bool copyu2 = u;// expected-error {{no viable conversion}}653int copy3 = b;// expected-error {{no viable conversion}}654int copy4 = n;655int copyu4 = u;// expected-error {{no viable conversion}}656const bool &copy5 = b;657const bool &copy6 = n;// expected-error {{no viable conversion}}658const bool &copyu6 = u;// expected-error {{no viable conversion}}659const int &copy7 = b;// expected-error {{no viable conversion}}660const int &copy8 = n;661const int &copyu8 = u;// expected-error {{no viable conversion}}662bool copyList1 = {b};663bool copyList2 = {n};// expected-error {{no viable conversion}}664bool copyListu2 = {u};// expected-error {{no viable conversion}}665int copyList3 = {b};// expected-error {{no viable conversion}}666int copyList4 = {n};667int copyListu4 = {u};// expected-error {{no viable conversion}}668const bool &copyList5 = {b};669const bool &copyList6 = {n};// expected-error {{no viable conversion}}670const bool &copyListu6 = {u};// expected-error {{no viable conversion}}671const int &copyList7 = {b};// expected-error {{no viable conversion}}672const int &copyList8 = {n};673const int &copyListu8 = {u};// expected-error {{no viable conversion}}674}675 676}677 678namespace deduction_guide2 {679 680template<typename T1 = int, typename T2 = int>681struct A {682  // expected-note@-1+ {{candidate template ignored}}683  // expected-note@-2+ {{implicit deduction guide}}684  explicit(!is_same<T1, T2>::value)685  A(T1 = 0, T2 = 0) {}686  // expected-note@-1 {{explicit constructor declared here}}687  // expected-note@-2 2{{explicit constructor is not a candidate}}688  // expected-note@-3 2{{implicit deduction guide declared}}689};690 691A a0 = 0;692A a1(0, 0);693A a2{0, 0};694A a3 = {0, 0};695 696A b0 = 0.0; // expected-error {{no viable constructor or deduction guide}}697A b1(0.0, 0.0);698A b2{0.0, 0.0};699A b3 = {0.0, 0.0};700 701A b4 = {0.0, 0}; // expected-error {{explicit constructor}}702 703template<typename T1, typename T2>704explicit A(T1, T2) -> A<T1, T2>;705// expected-note@-1+ {{explicit deduction guide}}706 707A c0 = 0;708A c1(0, 0);709A c2{0, 0};710A c3 = {0, 0};// expected-error {{explicit deduction guide}}711 712A d0 = 0.0; // expected-error {{no viable constructor or deduction guide}}713A d1(0, 0);714A d2{0, 0};715A d3 = {0.0, 0.0};// expected-error {{explicit deduction guide}}716 717}718 719namespace PR42980 {720using size_t = decltype(sizeof(0));721 722struct Str {// expected-note+ {{candidate constructor}}723  template <size_t N>724  explicit(N > 7)725  Str(char const (&str)[N]); // expected-note {{explicit constructor is not a candidate}}726};727 728template <size_t N>729Str::Str(char const(&str)[N]) { }730 731Str a = "short";732Str b = "not so short";// expected-error {{no viable conversion}}733 734}735 736namespace P1401 {737 738const int *ptr;739 740struct S {741  explicit(sizeof(char[2])) S(char); // expected-error {{explicit specifier argument evaluates to 2, which cannot be narrowed to type 'bool'}}742  explicit(ptr) S(long);             // expected-error {{conversion from 'const int *' to 'bool' is not allowed in a converted constant expression}}743  explicit(nullptr) S(int);          // expected-error {{conversion from 'std::nullptr_t' to 'bool' is not allowed in a converted constant expression}}744  explicit(42L) S(int, int);         // expected-error {{explicit specifier argument evaluates to 42, which cannot be narrowed to type 'bool'}}745  explicit(sizeof(char)) S();746  explicit(0) S(char, char);747  explicit(1L) S(char, char, char);748};749} // namespace P1401750