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
|
// RUN: %clang_cc1 -std=c++11 -verify %s -Wno-deprecated-builtins -Wno-defaulted-function-deleted
// RUN: %clang_cc1 -std=c++11 -verify %s -Wno-deprecated-builtins -Wno-defaulted-function-deleted -fclang-abi-compat=14 -DCLANG_ABI_COMPAT=14
// expected-no-diagnostics
template<typename T, bool B> struct trivially_copyable_check {
static_assert(B == __has_trivial_copy(T), "");
static_assert(B == __is_trivially_constructible(T, T), "");
static_assert(B == __is_trivially_constructible(T, const T &), "");
static_assert(B == __is_trivially_constructible(T, T &&), "");
typedef void type;
};
template<typename T> using trivially_copyable =
typename trivially_copyable_check<T, true>::type;
template<typename T> using not_trivially_copyable =
typename trivially_copyable_check<T, false>::type;
struct Trivial {};
using _ = trivially_copyable<Trivial>;
// A copy/move constructor for class X is trivial if it is not user-provided,
struct UserProvided {
UserProvided(const UserProvided &);
};
using _ = not_trivially_copyable<UserProvided>;
// its declared parameter type is the same as if it had been implicitly
// declared,
struct NonConstCopy {
NonConstCopy(NonConstCopy &) = default;
};
#if defined(CLANG_ABI_COMPAT) && CLANG_ABI_COMPAT <= 14
// Up until (and including) Clang 14, non-const copy constructors were not trivial because of dr2171
using _ = not_trivially_copyable<NonConstCopy>;
#else
// In the latest Clang version, all defaulted constructors are trivial, even if non-const, because
// dr2171 is fixed.
static_assert(__has_trivial_copy(NonConstCopy), "");
static_assert(__is_trivially_constructible(NonConstCopy, NonConstCopy &), "");
static_assert(!__is_trivially_constructible(NonConstCopy, NonConstCopy), "");
static_assert(!__is_trivially_constructible(NonConstCopy, const NonConstCopy &), "");
static_assert(!__is_trivially_constructible(NonConstCopy, NonConstCopy &&), "");
struct DefaultedSpecialMembers {
DefaultedSpecialMembers(const DefaultedSpecialMembers &) = default;
DefaultedSpecialMembers(DefaultedSpecialMembers &) = default;
DefaultedSpecialMembers(DefaultedSpecialMembers &&) = default;
};
using _ = trivially_copyable<DefaultedSpecialMembers>;
#endif
// class X has no virtual functions
struct VFn {
virtual void f();
};
using _ = not_trivially_copyable<VFn>;
// and no virtual base classes
struct VBase : virtual Trivial {};
using _ = not_trivially_copyable<VBase>;
// and the constructor selected to copy/move each [direct subobject] is trivial
struct TemplateCtor {
template<typename T> TemplateCtor(T &);
};
using _ = trivially_copyable<TemplateCtor>;
struct TemplateCtorMember {
TemplateCtor tc;
};
using _ = trivially_copyable<TemplateCtorMember>;
// We can select a non-trivial copy ctor even if there is a trivial one.
struct MutableTemplateCtorMember {
mutable TemplateCtor mtc;
};
static_assert(!__is_trivially_constructible(MutableTemplateCtorMember, const MutableTemplateCtorMember &), "");
static_assert(__is_trivially_constructible(MutableTemplateCtorMember, MutableTemplateCtorMember &&), "");
struct MutableTemplateCtorMember2 {
MutableTemplateCtorMember2(const MutableTemplateCtorMember2 &) = default;
MutableTemplateCtorMember2(MutableTemplateCtorMember2 &&) = default;
mutable TemplateCtor mtc;
};
static_assert(!__is_trivially_constructible(MutableTemplateCtorMember2, const MutableTemplateCtorMember2 &), "");
static_assert(__is_trivially_constructible(MutableTemplateCtorMember2, MutableTemplateCtorMember2 &&), "");
// Both trivial and non-trivial special members.
struct TNT {
TNT(const TNT &) = default; // trivial
TNT(TNT &); // non-trivial
TNT(TNT &&) = default; // trivial
TNT(const TNT &&); // non-trivial
};
static_assert(!__has_trivial_copy(TNT), "lie deliberately for gcc compatibility");
static_assert(__is_trivially_constructible(TNT, TNT), "");
static_assert(!__is_trivially_constructible(TNT, TNT &), "");
static_assert(__is_trivially_constructible(TNT, const TNT &), "");
static_assert(!__is_trivially_constructible(TNT, volatile TNT &), "");
static_assert(__is_trivially_constructible(TNT, TNT &&), "");
static_assert(!__is_trivially_constructible(TNT, const TNT &&), "");
static_assert(!__is_trivially_constructible(TNT, volatile TNT &&), "");
// This has only trivial special members.
struct DerivedFromTNT : TNT {};
static_assert(__has_trivial_copy(DerivedFromTNT), "");
static_assert(__is_trivially_constructible(DerivedFromTNT, DerivedFromTNT), "");
static_assert(__is_trivially_constructible(DerivedFromTNT, DerivedFromTNT &), "");
static_assert(__is_trivially_constructible(DerivedFromTNT, const DerivedFromTNT &), "");
static_assert(!__is_trivially_constructible(DerivedFromTNT, volatile DerivedFromTNT &), "");
static_assert(__is_trivially_constructible(DerivedFromTNT, DerivedFromTNT &&), "");
static_assert(__is_trivially_constructible(DerivedFromTNT, const DerivedFromTNT &&), "");
static_assert(!__is_trivially_constructible(DerivedFromTNT, volatile DerivedFromTNT &&), "");
// This has only trivial special members.
struct TNTMember {
TNT tnt;
};
static_assert(__has_trivial_copy(TNTMember), "");
static_assert(__is_trivially_constructible(TNTMember, TNTMember), "");
static_assert(__is_trivially_constructible(TNTMember, TNTMember &), "");
static_assert(__is_trivially_constructible(TNTMember, const TNTMember &), "");
static_assert(!__is_trivially_constructible(TNTMember, volatile TNTMember &), "");
static_assert(__is_trivially_constructible(TNTMember, TNTMember &&), "");
static_assert(__is_trivially_constructible(TNTMember, const TNTMember &&), "");
static_assert(!__is_trivially_constructible(TNTMember, volatile TNTMember &&), "");
struct NCCTNT : NonConstCopy, TNT {};
static_assert(!__has_trivial_copy(NCCTNT), "");
static_assert(!__is_trivially_constructible(NCCTNT, NCCTNT), "");
static_assert(!__is_trivially_constructible(NCCTNT, NCCTNT &), "");
static_assert(!__is_trivially_constructible(NCCTNT, const NCCTNT &), "");
static_assert(!__is_trivially_constructible(NCCTNT, volatile NCCTNT &), "");
static_assert(!__is_trivially_constructible(NCCTNT, NCCTNT &&), "");
static_assert(!__is_trivially_constructible(NCCTNT, const NCCTNT &&), "");
static_assert(!__is_trivially_constructible(NCCTNT, volatile NCCTNT &&), "");
struct TemplateCtorNoMove {
TemplateCtorNoMove(const TemplateCtorNoMove &) = default;
template<typename T> TemplateCtorNoMove(T &&);
};
static_assert(__is_trivially_constructible(TemplateCtorNoMove, const TemplateCtorNoMove &), "");
static_assert(!__is_trivially_constructible(TemplateCtorNoMove, TemplateCtorNoMove &&), "");
struct UseTemplateCtorNoMove {
TemplateCtorNoMove tcnm;
};
static_assert(__is_trivially_constructible(UseTemplateCtorNoMove, const UseTemplateCtorNoMove &), "");
static_assert(!__is_trivially_constructible(UseTemplateCtorNoMove, UseTemplateCtorNoMove &&), "");
struct TemplateCtorNoMoveSFINAE {
TemplateCtorNoMoveSFINAE(const TemplateCtorNoMoveSFINAE &) = default;
template<typename T, typename U = typename T::error> TemplateCtorNoMoveSFINAE(T &&);
};
static_assert(__is_trivially_constructible(TemplateCtorNoMoveSFINAE, const TemplateCtorNoMoveSFINAE &), "");
static_assert(__is_trivially_constructible(TemplateCtorNoMoveSFINAE, TemplateCtorNoMoveSFINAE &&), "");
struct UseTemplateCtorNoMoveSFINAE {
TemplateCtorNoMoveSFINAE tcnm;
};
static_assert(__is_trivially_constructible(UseTemplateCtorNoMoveSFINAE, const UseTemplateCtorNoMoveSFINAE &), "");
static_assert(__is_trivially_constructible(UseTemplateCtorNoMoveSFINAE, UseTemplateCtorNoMoveSFINAE &&), "");
namespace TrivialityDependsOnImplicitDeletion {
struct PrivateMove {
PrivateMove(const PrivateMove &) = default;
private:
PrivateMove(PrivateMove &&);
friend class Access;
};
static_assert(__is_trivially_constructible(PrivateMove, const PrivateMove &), "");
static_assert(!__is_trivially_constructible(PrivateMove, PrivateMove &&), "");
struct NoAccess {
PrivateMove pm;
// NoAccess's move is deleted, so moves of it use PrivateMove's copy ctor,
// which is trivial.
};
static_assert(__is_trivially_constructible(NoAccess, const NoAccess &), "");
static_assert(__is_trivially_constructible(NoAccess, NoAccess &&), "");
struct TopNoAccess : NoAccess {};
static_assert(__is_trivially_constructible(TopNoAccess, const TopNoAccess &), "");
static_assert(__is_trivially_constructible(TopNoAccess, TopNoAccess &&), "");
struct Access {
PrivateMove pm;
// NoAccess's move would *not* be deleted, so is *not* suppressed,
// so moves of it use PrivateMove's move ctor, which is not trivial.
};
static_assert(__is_trivially_constructible(Access, const Access &), "");
static_assert(!__is_trivially_constructible(Access, Access &&), "");
struct TopAccess : Access {};
static_assert(__is_trivially_constructible(TopAccess, const TopAccess &), "");
static_assert(!__is_trivially_constructible(TopAccess, TopAccess &&), "");
}
namespace TrivialityDependsOnDestructor {
class HasInaccessibleDestructor { ~HasInaccessibleDestructor() = default; };
struct HasImplicitlyDeletedDestructor : HasInaccessibleDestructor {};
struct HasImplicitlyDeletedCopyCtor : HasImplicitlyDeletedDestructor {
HasImplicitlyDeletedCopyCtor() = default;
template<typename T> HasImplicitlyDeletedCopyCtor(T &&);
// Copy ctor is deleted but trivial.
// Move ctor is suppressed.
HasImplicitlyDeletedCopyCtor(const HasImplicitlyDeletedCopyCtor&) = default;
HasImplicitlyDeletedCopyCtor(HasImplicitlyDeletedCopyCtor&&) = default;
};
struct Test : HasImplicitlyDeletedCopyCtor {
Test(const Test&) = default;
Test(Test&&) = default;
};
// Implicit copy ctor calls deleted trivial copy ctor.
static_assert(__has_trivial_copy(Test), "");
// This is false because the destructor is deleted.
static_assert(!__is_trivially_constructible(Test, const Test &), "");
// Implicit move ctor calls template ctor.
static_assert(!__is_trivially_constructible(Test, Test &&), "");
struct HasAccessibleDestructor { ~HasAccessibleDestructor() = default; };
struct HasImplicitlyDefaultedDestructor : HasAccessibleDestructor {};
struct HasImplicitlyDefaultedCopyCtor : HasImplicitlyDefaultedDestructor {
template<typename T> HasImplicitlyDefaultedCopyCtor(T &&);
// Copy ctor is trivial.
// Move ctor is trivial.
};
struct Test2 : HasImplicitlyDefaultedCopyCtor {};
// Implicit copy ctor calls trivial copy ctor.
static_assert(__has_trivial_copy(Test2), "");
static_assert(__is_trivially_constructible(Test2, const Test2 &), "");
// Implicit move ctor calls trivial move ctor.
static_assert(__is_trivially_constructible(Test2, Test2 &&), "");
}
|