| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // See [constexpr specifier](https://en.cppreference.com/w/cpp/language/constexpr) | ||
| 2 | // See [static members](https://en.cppreference.com/w/cpp/language/static) | ||
| 3 | // See [ODR-use](https://en.cppreference.com/w/cpp/language/definition#ODR-use) | ||
| 4 | |||
| 5 | // NOLINTBEGIN(*-avoid-const-or-ref-data-members) | ||
| 6 | |||
| 7 | class Const { | ||
| 8 | public: | ||
| 9 | 1 | Const() noexcept : uninitialized_{1} {} | |
| 10 | const int uninitialized_; // must be initialized in constructor NOLINT(*-use-default-member-init) | ||
| 11 | const int initialized_{2}; // may be initialized in constructor | ||
| 12 | }; | ||
| 13 | |||
| 14 | // NOLINTEND(*-avoid-const-or-ref-data-members) | ||
| 15 | |||
| 16 | class StaticConst { | ||
| 17 | public: | ||
| 18 | static const int uninitialized_; // initialized later | ||
| 19 | static const int initialized_{4}; | ||
| 20 | static const int unused_{5}; | ||
| 21 | }; | ||
| 22 | |||
| 23 | // a definition outside the class is required if odr-used | ||
| 24 | const int StaticConst::uninitialized_{3}; | ||
| 25 | const int StaticConst::initialized_; // already initialized | ||
| 26 | // const int StaticConst::unused_ ; // already initialized | ||
| 27 | |||
| 28 | // must be initialized after the constant upon which it depends | ||
| 29 | static const int dependent_non_member{StaticConst::uninitialized_ + 100}; | ||
| 30 | |||
| 31 | class StaticConst2 { | ||
| 32 | public: | ||
| 33 | static const int dependent_{StaticConst::initialized_ + 100}; | ||
| 34 | static const int dependent2_{dependent_non_member + 100}; | ||
| 35 | }; | ||
| 36 | |||
| 37 | // a definition outside the class is required if odr-used | ||
| 38 | const int StaticConst2::dependent_; | ||
| 39 | const int StaticConst2::dependent2_; | ||
| 40 | |||
| 41 | #if __cplusplus >= 201703L // since C++17 | ||
| 42 | |||
| 43 | class ConstExpr { | ||
| 44 | public: | ||
| 45 | // constexpr int non_static_ { 0 }; // must be static | ||
| 46 | // static constexpr int uninitialized_; // must be initialized | ||
| 47 | static constexpr int initialized_{2}; | ||
| 48 | }; | ||
| 49 | |||
| 50 | #endif // C++17 | ||
| 51 | |||
| 52 | // a definition outside the class is optional | ||
| 53 | // constexpr int ConstExpr::initialized_; // already initialized | ||
| 54 | |||
| 55 | //////////////////////////////////////////////////////////////////////////////// | ||
| 56 | |||
| 57 | #include <gtest/gtest.h> | ||
| 58 | #include <vector> | ||
| 59 | |||
| 60 | 8 | TEST(Const, const) { | |
| 61 | 2 | auto instance = Const{}; | |
| 62 |
2/10✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
2 | EXPECT_EQ(1, instance.uninitialized_); |
| 63 |
2/10✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
2 | EXPECT_EQ(2, instance.initialized_); |
| 64 | 2 | } | |
| 65 | |||
| 66 | 8 | TEST(Const, static_const) { | |
| 67 |
2/10✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
2 | EXPECT_EQ(3, StaticConst::uninitialized_); |
| 68 |
2/10✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
2 | EXPECT_EQ(4, StaticConst::initialized_); |
| 69 | 2 | } | |
| 70 | |||
| 71 | 8 | TEST(Const, static_dependent) { | |
| 72 |
2/10✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
2 | EXPECT_EQ(104, StaticConst2::dependent_); |
| 73 |
2/10✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
2 | EXPECT_EQ(203, StaticConst2::dependent2_); |
| 74 | 2 | } | |
| 75 | |||
| 76 | #if __cplusplus >= 201703L // since C++17 | ||
| 77 | |||
| 78 |
2/10✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
10 | TEST(Const, constexpr) { EXPECT_EQ(2, ConstExpr::initialized_); } |
| 79 | |||
| 80 | 8 | TEST(Const, constexpr_odr_used) { | |
| 81 | 2 | auto v = std::vector<int>{}; | |
| 82 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | v.push_back(ConstExpr::initialized_); |
| 83 | 2 | const auto& ref = ConstExpr::initialized_; | |
| 84 |
2/10✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
2 | EXPECT_EQ(2, ref); |
| 85 | 4 | } | |
| 86 | |||
| 87 | #endif // C++17 | ||
| 88 |