xref: /llvm-project/clang/test/CXX/temp/temp.decls/temp.mem/p5.cpp (revision ba15d186e5cef2620d562c6c9d9a6d570382cd0a)
1 // RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify=expected,cxx23    %s
2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=expected,cxx98_20 %s
3 // RUN: %clang_cc1 -std=c++98 -fsyntax-only -verify=expected,cxx98_20 %s
4 // RUN: %clang_cc1            -fsyntax-only -verify=expected,cxx98_20 %s
5 
6 struct A {
7   template <class T> operator T*();
8 };
9 
operator T*()10 template <class T> A::operator T*() { return 0; }
operator char*()11 template <> A::operator char*(){ return 0; } // specialization
12 template A::operator void*(); // explicit instantiation
13 
main()14 int main() {
15   A a;
16   int *ip;
17   ip = a.operator int*();
18 }
19 
20 // PR5742
21 namespace PR5742 {
22   template <class T> struct A { };
23   template <class T> struct B { };
24 
25   struct S {
26     template <class T> operator T();
27   } s;
28 
f()29   void f() {
30     s.operator A<A<int> >();
31     s.operator A<B<int> >();
32     s.operator A<B<A<int> > >();
33   }
34 }
35 
36 // PR5762
37 class Foo {
38  public:
39   template <typename T> operator T();
40 
41   template <typename T>
As()42   T As() {
43     return this->operator T();
44   }
45 
46   template <typename T>
As2()47   T As2() {
48     return operator T();
49   }
50 
AsInt()51   int AsInt() {
52     return this->operator int();
53   }
54 };
55 
56 template float Foo::As();
57 template double Foo::As2();
58 
59 // Partial ordering with conversion function templates.
60 struct X0 {
operator T*X061   template<typename T> operator T*() {
62     T x = 1; // expected-note{{variable 'x' declared const here}}
63     x = 17; // expected-error{{cannot assign to variable 'x' with const-qualified type 'const int'}}
64   }
65 
66   template<typename T> operator T*() const; // expected-note{{explicit instantiation refers here}}
67 
operator const T*X068   template<typename T> operator const T*() const {
69     T x = T();
70     return x; // cxx98_20-error{{cannot initialize return object of type 'const char *' with an lvalue of type 'char'}} \
71     // cxx98_20-error{{cannot initialize return object of type 'const int *' with an lvalue of type 'int'}} \
72     // cxx23-error{{cannot initialize return object of type 'const char *' with an rvalue of type 'char'}} \
73     // cxx23-error{{cannot initialize return object of type 'const int *' with an rvalue of type 'int'}}
74   }
75 };
76 
77 template X0::operator const char*() const; // expected-note{{'X0::operator const char *<char>' requested here}}
78 template X0::operator const int*(); // expected-note{{'X0::operator const int *<const int>' requested here}}
79 template X0::operator float*() const; // expected-error{{explicit instantiation of undefined function template 'operator type-parameter-0-0 *'}}
80 
test_X0(X0 x0,const X0 & x0c)81 void test_X0(X0 x0, const X0 &x0c) {
82   x0.operator const int*(); // expected-note{{in instantiation of function template specialization}}
83   x0.operator float *();
84   x0c.operator const char*();
85 }
86 
87 namespace PR14211 {
88 template <class U> struct X {
fooPR14211::X89   void foo(U){}
fooPR14211::X90   template <class T> void foo(T){}
91 
barPR14211::X92   template <class T> void bar(T){}
barPR14211::X93   void bar(U){}
94 };
95 
96 template void X<int>::foo(int);
97 template void X<int>::bar(int);
98 }
99