| 1 |
// RUN: %clang_cc1 -std=c++20 -verify %s |
| 2 |
// RUN: not %clang_cc1 -std=c++20 -fsyntax-only -fno-diagnostics-show-line-numbers -fcaret-diagnostics-max-lines=1 %s 2>&1 | FileCheck %s -strict-whitespace |
| 3 |
|
| 4 |
namespace GH46386 { |
| 5 |
extern "C" { // expected-note {{extern "C" language linkage specification begins here}} |
| 6 |
|
| 7 |
// CHECK: error: templates must have C++ linkage |
| 8 |
// CHECK-NEXT: {{^}} void f(auto) {} |
| 9 |
// CHECK-NEXT: {{^}} ^~~~~~~~~~~~{{$}} |
| 10 |
void f(auto) {} // expected-error {{templates must have C++ linkage}} |
| 11 |
|
| 12 |
void f(void) { // expected-note {{candidate function not viable: requires 0 arguments, but 1 was provided}} |
| 13 |
f(1); // expected-error {{no matching function for call to 'f'}} |
| 14 |
} |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
// Templates and partial and explicit specializations can't have C linkage. |
| 19 |
namespace extern_c_templates { |
| 20 |
|
| 21 |
template<typename T> struct A { |
| 22 |
static int a; |
| 23 |
struct b; |
| 24 |
void c(); |
| 25 |
enum class d; |
| 26 |
|
| 27 |
template<typename U> static int e; |
| 28 |
template<typename U> struct f; |
| 29 |
template<typename U> void g(); |
| 30 |
}; |
| 31 |
|
| 32 |
template<typename T> int B; |
| 33 |
template<typename T> void C(); |
| 34 |
|
| 35 |
extern "C" { // expected-note 1+{{begins here}} |
| 36 |
// templates |
| 37 |
template<typename T> struct A; // expected-error {{templates must have C++ linkage}} |
| 38 |
template<typename T> int B; // expected-error {{templates must have C++ linkage}} |
| 39 |
template<typename T> void C(); // expected-error {{templates must have C++ linkage}} |
| 40 |
|
| 41 |
// non-template members of a template |
| 42 |
// FIXME: Should these really be valid? |
| 43 |
template<typename T> int A<T>::a; |
| 44 |
template<typename T> struct A<T>::b {}; |
| 45 |
template<typename T> void A<T>::c() {} |
| 46 |
template<typename T> enum class A<T>::d {}; |
| 47 |
|
| 48 |
// templates |
| 49 |
template<typename T> template<typename U> int A<T>::e; // expected-error {{templates must have C++ linkage}} |
| 50 |
template<typename T> template<typename U> struct A<T>::f {}; // expected-error {{templates must have C++ linkage}} |
| 51 |
template<typename T> template<typename U> void A<T>::g() {} // expected-error {{templates must have C++ linkage}} |
| 52 |
|
| 53 |
// partial specializations |
| 54 |
template<typename T> struct A<int*>; // expected-error {{templates must have C++ linkage}} |
| 55 |
template<typename T> int B<int*>; // expected-error {{templates must have C++ linkage}} |
| 56 |
template<typename T> template<typename U> int A<T>::e<U*>; // expected-error {{templates must have C++ linkage}} |
| 57 |
template<typename T> template<typename U> struct A<T>::f<U*> {}; // expected-error {{templates must have C++ linkage}} |
| 58 |
|
| 59 |
// explicit specializations of templates |
| 60 |
template<> struct A<char> {}; // expected-error {{templates must have C++ linkage}} |
| 61 |
template<> int B<char>; // expected-error {{templates must have C++ linkage}} |
| 62 |
template<> void C<char>() {} // expected-error {{templates must have C++ linkage}} |
| 63 |
|
| 64 |
// explicit specializations of members of a template |
| 65 |
template<> int A<int>::a; // expected-error {{templates must have C++ linkage}} |
| 66 |
template<> struct A<int>::b {}; // expected-error {{templates must have C++ linkage}} |
| 67 |
template<> void A<int>::c() {} // expected-error {{templates must have C++ linkage}} |
| 68 |
template<> enum class A<int>::d {}; // expected-error {{templates must have C++ linkage}} |
| 69 |
|
| 70 |
// explicit specializations of member templates |
| 71 |
template<> template<typename U> int A<int>::e; // expected-error {{templates must have C++ linkage}} |
| 72 |
template<> template<typename U> struct A<int>::f {}; // expected-error {{templates must have C++ linkage}} |
| 73 |
template<> template<typename U> void A<int>::g() {} // expected-error {{templates must have C++ linkage}} |
| 74 |
} |
| 75 |
|
| 76 |
// Provide valid definitions for the explicit instantiations below. |
| 77 |
// FIXME: Our recovery from the invalid definitions above isn't very good. |
| 78 |
template<typename T> template<typename U> int A<T>::e; |
| 79 |
template<typename T> template<typename U> struct A<T>::f {}; |
| 80 |
template<typename T> template<typename U> void A<T>::g() {} |
| 81 |
|
| 82 |
extern "C" { |
| 83 |
// explicit instantiations |
| 84 |
// FIXME: Should these really be valid? |
| 85 |
template struct A<double>; |
| 86 |
template int A<float>::a; |
| 87 |
template struct A<float>::b; |
| 88 |
template void A<float>::c(); |
| 89 |
template int A<float>::e<float>; |
| 90 |
template struct A<float>::f<float>; |
| 91 |
template void A<float>::g<float>(); |
| 92 |
} |
| 93 |
|
| 94 |
} |