| 1 |
// { dg-additional-options "-fmodule-header" } |
| 2 |
// { dg-module-cmi !{} } |
| 3 |
// external linkage variables or functions in header units must |
| 4 |
// not have non-inline definitions |
| 5 |
|
| 6 |
struct S { int x; }; |
| 7 |
|
| 8 |
int x_err; // { dg-error "external linkage definition" } |
| 9 |
int y_err = 123; // { dg-error "external linkage definition" } |
| 10 |
auto [d_err] = S{}; // { dg-error "external linkage definition" } |
| 11 |
void f_err() {} // { dg-error "external linkage definition" } |
| 12 |
|
| 13 |
struct Err { |
| 14 |
Err(); |
| 15 |
void m(); |
| 16 |
static void s(); |
| 17 |
static int x; |
| 18 |
static int y; |
| 19 |
}; |
| 20 |
Err::Err() = default; // { dg-error "external linkage definition" } |
| 21 |
void Err::m() {} // { dg-error "external linkage definition" } |
| 22 |
void Err::s() {} // { dg-error "external linkage definition" } |
| 23 |
int Err::x; // { dg-error "external linkage definition" } |
| 24 |
int Err::y = 123; // { dg-error "external linkage definition" } |
| 25 |
|
| 26 |
// No definition, OK |
| 27 |
extern int y_decl; |
| 28 |
void f_decl(); |
| 29 |
|
| 30 |
template <typename> struct DeductionGuide {}; |
| 31 |
DeductionGuide() -> DeductionGuide<int>; |
| 32 |
|
| 33 |
struct NoDefStatics { |
| 34 |
enum E { V }; |
| 35 |
static const int x = 123; |
| 36 |
static const E e = V; |
| 37 |
}; |
| 38 |
|
| 39 |
// But these have definitions again (though the error locations aren't great) |
| 40 |
struct YesDefStatics { |
| 41 |
enum E { V }; |
| 42 |
static const int x = 123; // { dg-error "external linkage definition" } |
| 43 |
static const E e = V; // { dg-error "external linkage definition" } |
| 44 |
}; |
| 45 |
const int YesDefStatics::x; |
| 46 |
const YesDefStatics::E YesDefStatics::e; |
| 47 |
|
| 48 |
// Inline decls are OK |
| 49 |
inline int x_inl; |
| 50 |
inline int y_inl = 123; |
| 51 |
inline void f_inl() {} |
| 52 |
constexpr void g_inl() {} |
| 53 |
void h_inl() = delete; |
| 54 |
|
| 55 |
struct Inl { |
| 56 |
void m() {} |
| 57 |
static void s() {} |
| 58 |
static inline int x; |
| 59 |
static inline int y = 123; |
| 60 |
}; |
| 61 |
|
| 62 |
// Internal linkage decls are OK |
| 63 |
static int x_internal; |
| 64 |
static int y_internal = 123; |
| 65 |
namespace { auto [d_internal] = S{}; } |
| 66 |
static void f_internal() {} |
| 67 |
|
| 68 |
namespace { |
| 69 |
struct Internal { |
| 70 |
void m(); |
| 71 |
static void s(); |
| 72 |
static int x; |
| 73 |
static int y; |
| 74 |
}; |
| 75 |
void Internal::m() {} |
| 76 |
void Internal::s() {} |
| 77 |
int Internal::x; |
| 78 |
int Internal::y = 123; |
| 79 |
} |
| 80 |
|
| 81 |
// Function-scope entities are OK |
| 82 |
inline void f_static() { |
| 83 |
static int x_static; |
| 84 |
static int y_static = 123; |
| 85 |
thread_local int x_thread_local; |
| 86 |
thread_local int y_thread_local = 123; |
| 87 |
|
| 88 |
#if __cplusplus >= 202002L |
| 89 |
static auto [d_static] = S{}; |
| 90 |
thread_local auto [d_thread_local] = S{}; |
| 91 |
#endif |
| 92 |
|
| 93 |
x_static = y_static; |
| 94 |
x_thread_local = y_thread_local; |
| 95 |
} |
| 96 |
|
| 97 |
// Templates (not functions or variables) are OK |
| 98 |
template <typename> int x_tpl; |
| 99 |
template <typename> int y_tpl = 123; |
| 100 |
template <typename> void f_tpl() {} |
| 101 |
|
| 102 |
struct Template_Body { |
| 103 |
template <typename> void m(); |
| 104 |
template <typename> static void s(); |
| 105 |
template <typename> static int x; |
| 106 |
template <typename> static int y; |
| 107 |
}; |
| 108 |
template <typename> void Template_Body::m() {} |
| 109 |
template <typename> void Template_Body::s() {} |
| 110 |
template <typename> int Template_Body::x; |
| 111 |
template <typename> int Template_Body::y = 123; |
| 112 |
|
| 113 |
template <typename> struct Template_Type { |
| 114 |
void m(); |
| 115 |
static void s(); |
| 116 |
static int x; |
| 117 |
static int y; |
| 118 |
}; |
| 119 |
template <typename T> void Template_Type<T>::m() {} |
| 120 |
template <typename T> void Template_Type<T>::s() {} |
| 121 |
template <typename T> int Template_Type<T>::x; |
| 122 |
template <typename T> int Template_Type<T>::y = 123; |
| 123 |
|
| 124 |
// Implicit instantiations are OK |
| 125 |
inline void instantiate_tmpls() { |
| 126 |
x_tpl<int> = y_tpl<int>; |
| 127 |
f_tpl<int>(); |
| 128 |
|
| 129 |
Template_Body{}.m<int>(); |
| 130 |
Template_Body::s<int>(); |
| 131 |
Template_Body::x<int> = Template_Body::y<int>; |
| 132 |
|
| 133 |
using TT = Template_Type<int>; |
| 134 |
TT{}.m(); |
| 135 |
TT::s(); |
| 136 |
TT::x = TT::y; |
| 137 |
} |
| 138 |
|
| 139 |
// Explicit instantiations are also OK (extern or otherwise) |
| 140 |
template int x_tpl<char>; |
| 141 |
template int y_tpl<char>; |
| 142 |
template void f_tpl<char>(); |
| 143 |
|
| 144 |
template void Template_Body::m<char>(); |
| 145 |
template void Template_Body::s<char>(); |
| 146 |
template int Template_Body::x<char>; |
| 147 |
template int Template_Body::y<char>; |
| 148 |
|
| 149 |
template void Template_Type<char>::m(); |
| 150 |
template void Template_Type<char>::s(); |
| 151 |
template int Template_Type<char>::x; |
| 152 |
template int Template_Type<char>::y; |
| 153 |
|
| 154 |
extern template int x_tpl<double>; |
| 155 |
extern template int y_tpl<double>; |
| 156 |
extern template void f_tpl<double>(); |
| 157 |
|
| 158 |
extern template void Template_Body::m<double>(); |
| 159 |
extern template void Template_Body::s<double>(); |
| 160 |
extern template int Template_Body::x<double>; |
| 161 |
extern template int Template_Body::y<double>; |
| 162 |
|
| 163 |
extern template void Template_Type<double>::m(); |
| 164 |
extern template void Template_Type<double>::s(); |
| 165 |
extern template int Template_Type<double>::x; |
| 166 |
extern template int Template_Type<double>::y; |
| 167 |
|
| 168 |
// But explicit specialisations are not (though note [temp.expl.spec] p13) |
| 169 |
template <> int x_tpl<short>; // { dg-error "inline" } |
| 170 |
template <> int y_tpl<short> = 123; // { dg-error "inline" } |
| 171 |
template <> void f_tpl<short>() {} // { dg-error "inline" } |
| 172 |
|
| 173 |
template <> void Template_Body::m<short>() {} // { dg-error "inline" } |
| 174 |
template <> void Template_Body::s<short>() {} // { dg-error "inline" } |
| 175 |
template <> int Template_Body::x<short>; // { dg-bogus "inline" "not a definition" } |
| 176 |
template <> int Template_Body::y<short> = 123; // { dg-error "inline" } |
| 177 |
|
| 178 |
template <> void Template_Type<short>::m() {} // { dg-error "inline" } |
| 179 |
template <> void Template_Type<short>::s() {} // { dg-error "inline" } |
| 180 |
template <> int Template_Type<short>::x; // { dg-bogus "inline" "not a definition" } |
| 181 |
template <> int Template_Type<short>::y = 123; // { dg-error "inline" } |