-
Notifications
You must be signed in to change notification settings - Fork 0
/
literal_test.cpp
69 lines (62 loc) · 1.38 KB
/
literal_test.cpp
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
#include "iostream"
#include "vector"
#include "string"
#define LITERAL_TEST(type) \
std::cout << std::is_literal_type<type>::value << " " #type << std::endl
class A {
char a[2];
static int *p;
constexpr A() : a("") { };
};
class B {
int a;
static int count;
};
struct C {
/** 该构造函数是使C非constexpr class的原因
* 满足将所有的成员初始化、body为空的条件,但不能用constexpr修饰该构造函数,
* 因为id的初始化值非 const expression
*/
C() : id(++count), foo(1) { }
static constexpr int &getCount() { return count; }
const int &getId() const { return id; }
private:
static int count;
int id, foo;
};
int C::count = 0;
int gv;
struct D {
constexpr D() : id(1), foo(1) { }
static constexpr int &getCount() {
return count; }
const int &getId() const { return id; }
private:
static int count;
int id, foo;
};
int D::count = 0;
class E {
int i, j;
public:
constexpr E() : i(0), j(0) { }
};
int main() {
LITERAL_TEST(int);
LITERAL_TEST(std::string);
LITERAL_TEST(std::vector<int>);
LITERAL_TEST(int[]);
LITERAL_TEST(char[]);
LITERAL_TEST(int &);
LITERAL_TEST(int *);
LITERAL_TEST(A);
LITERAL_TEST(B);
LITERAL_TEST(C);
LITERAL_TEST(D);
C a, b, c;
std::cout << std::endl;
std::cout << C::getCount() << std::endl;
std::cout << a.getId() << std::endl;
std::cout << b.getId() << std::endl;
std::cout << c.getId() << std::endl;
}