C++: test for Class::isStandardLayout()

This commit is contained in:
Nick Rolfe
2018-09-18 15:32:44 +01:00
parent e5b9dca312
commit f1358b7c02
4 changed files with 120 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
// Confirm that `Class::isStandardLayout()` holds for a C struct.
struct PlainOldCStruct {
int x;
};

View File

@@ -0,0 +1,89 @@
// AStd is a standard layout type
struct AStd {
int x;
};
// BNonStd is NOT a standard layout type - not all members have the same access
// control
class BNonStd {
int x;
public:
int y;
};
// CNonStd is NOT a standard layout type - it has a virtual function
class CNonStd {
virtual void f();
};
// DNonStd is NOT a standard layout type - it has a virtual base class
class DNonStd : public virtual AStd {};
// ENonStd is NOT a standard layout type - it has a data member of reference
// type
class ENonStd {
int& xref;
};
// FStd is a standard layout type - all data members are standard layout types
class FStd {
AStd a;
};
// GNonStd is NOT a standard layout type - contains a non-standard-layout member
class GNonStd {
BNonStd b;
};
// HStd is a standard layout type - its base class is a standard layout type
struct HStd : AStd {};
// INonStd is NOT a standard layout type - its base class is not a standard
// layout type
struct INonStd : BNonStd {};
// JStd is a standard layout type
struct JStd {
static int x;
};
// KStd is a standard layout type - base class has no non-static data members
struct KStd : JStd {};
// LStd is a standard layout type - only one base class has non-static data
// members
struct LStd : AStd, JStd {};
// MNonStd is NOT a standard layout type - more than one base class with
// non-static data members
struct MNonStd : AStd, FStd {};
// Instantiations of NMaybeStd may or may not be standard layout types,
// depending on the template parameter.
template<typename T>
struct NMaybeStd {
T x;
};
// Instantiation NMaybeStd<AStd> is a standard layout type
NMaybeStd<AStd> nmaybestd_astd;
// Instantiation NMaybeStd<AStd> is a standard layout type
NMaybeStd<int> nmaybestd_int;
// Instantiation NMaybeStd<BNonStd> is NOT a standard layout type
NMaybeStd<BNonStd> nmaybestd_bnonstd;
// Instantiations of ONonStd cannot be standard layout types - regardless of the
// template parameter's type - since not all members have the same access
// control.
template<typename T>
struct ONonStd {
T x;
private:
T y;
};
// Therefore instantiation ONonStd<int> is NOT a standard layout type
ONonStd<int> ononstd_int;

View File

@@ -0,0 +1,21 @@
| file://:0:0:0:0 | __va_list_tag | standard layout |
| test.c:3:8:3:22 | PlainOldCStruct | standard layout |
| test.cpp:3:8:3:11 | AStd | standard layout |
| test.cpp:9:7:9:13 | BNonStd | NOT standard layout |
| test.cpp:16:7:16:13 | CNonStd | NOT standard layout |
| test.cpp:21:7:21:13 | DNonStd | NOT standard layout |
| test.cpp:25:7:25:13 | ENonStd | NOT standard layout |
| test.cpp:30:7:30:10 | FStd | standard layout |
| test.cpp:35:7:35:13 | GNonStd | NOT standard layout |
| test.cpp:40:8:40:11 | HStd | standard layout |
| test.cpp:44:8:44:14 | INonStd | NOT standard layout |
| test.cpp:47:8:47:11 | JStd | standard layout |
| test.cpp:52:8:52:11 | KStd | standard layout |
| test.cpp:56:8:56:11 | LStd | standard layout |
| test.cpp:60:8:60:14 | MNonStd | NOT standard layout |
| test.cpp:65:8:65:16 | NMaybeStd<AStd> | standard layout |
| test.cpp:65:8:65:16 | NMaybeStd<BNonStd> | NOT standard layout |
| test.cpp:65:8:65:16 | NMaybeStd<T> | NOT standard layout |
| test.cpp:65:8:65:16 | NMaybeStd<int> | standard layout |
| test.cpp:82:8:82:14 | ONonStd<T> | NOT standard layout |
| test.cpp:82:8:82:14 | ONonStd<int> | NOT standard layout |

View File

@@ -0,0 +1,5 @@
import cpp
from Class c, string s
where if c.isStandardLayout() then s = "standard layout" else s = "NOT standard layout"
select c, s