Skip to content

Commit 7ef0f9f

Browse files
dota17baylesj
authored andcommitted
[Language Conformance] Use constexpr restriction in jsoncpp (open-source-parsers#1005)
* use constexpr restriction in jsoncpp * remove TODO comment
1 parent 3550a0a commit 7ef0f9f

File tree

3 files changed

+18
-34
lines changed

3 files changed

+18
-34
lines changed

include/json/value.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,31 +203,33 @@ class JSON_API Value {
203203
static Value const& nullSingleton();
204204

205205
/// Minimum signed integer value that can be stored in a Json::Value.
206-
static const LargestInt minLargestInt;
206+
static constexpr LargestInt minLargestInt = LargestInt(~(LargestUInt(-1) / 2));
207207
/// Maximum signed integer value that can be stored in a Json::Value.
208-
static const LargestInt maxLargestInt;
208+
static constexpr LargestInt maxLargestInt = LargestInt(LargestUInt(-1) / 2);
209209
/// Maximum unsigned integer value that can be stored in a Json::Value.
210-
static const LargestUInt maxLargestUInt;
210+
static constexpr LargestUInt maxLargestUInt = LargestUInt(-1);
211211

212212
/// Minimum signed int value that can be stored in a Json::Value.
213-
static const Int minInt;
213+
static constexpr Int minInt = Int(~(UInt(-1) / 2));
214214
/// Maximum signed int value that can be stored in a Json::Value.
215-
static const Int maxInt;
215+
static constexpr Int maxInt = Int(UInt(-1) / 2);
216216
/// Maximum unsigned int value that can be stored in a Json::Value.
217-
static const UInt maxUInt;
217+
static constexpr UInt maxUInt = UInt(-1);
218218

219219
#if defined(JSON_HAS_INT64)
220220
/// Minimum signed 64 bits int value that can be stored in a Json::Value.
221-
static const Int64 minInt64;
221+
static constexpr Int64 minInt64 = Int64(~(UInt64(-1) / 2));
222222
/// Maximum signed 64 bits int value that can be stored in a Json::Value.
223-
static const Int64 maxInt64;
223+
static constexpr Int64 maxInt64 = Int64(UInt64(-1) / 2);
224224
/// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
225-
static const UInt64 maxUInt64;
225+
static constexpr UInt64 maxUInt64 = UInt64(-1);
226226
#endif // defined(JSON_HAS_INT64)
227-
228227
/// Default precision for real value for string representation.
229-
static const UInt defaultRealPrecision;
230-
228+
static constexpr UInt defaultRealPrecision = 17;
229+
// The constant is hard-coded because some compiler have trouble
230+
// converting Value::maxUInt64 to a double correctly (AIX/xlC).
231+
// Assumes that UInt64 is a 64 bits integer.
232+
static constexpr double maxUInt64AsDouble = 18446744073709551615.0;
231233
// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler
232234
// when using gcc and clang backend compilers. CZString
233235
// cannot be defined as private. See issue #486

src/lib_json/json_reader.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,12 +1548,11 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
15481548
if (isNegative)
15491549
++current;
15501550

1551-
// TODO(issue #960): Change to constexpr
1552-
static const auto positive_threshold = Value::maxLargestUInt / 10;
1553-
static const auto positive_last_digit = Value::maxLargestUInt % 10;
1554-
static const auto negative_threshold =
1551+
static constexpr auto positive_threshold = Value::maxLargestUInt / 10;
1552+
static constexpr auto positive_last_digit = Value::maxLargestUInt % 10;
1553+
static constexpr auto negative_threshold =
15551554
Value::LargestUInt(Value::minLargestInt) / 10;
1556-
static const auto negative_last_digit =
1555+
static constexpr auto negative_last_digit =
15571556
Value::LargestUInt(Value::minLargestInt) % 10;
15581557

15591558
const auto threshold = isNegative ? negative_threshold : positive_threshold;

src/lib_json/json_value.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,6 @@ Value const& Value::null = Value::nullSingleton();
8888
Value const& Value::nullRef = Value::nullSingleton();
8989
#endif
9090

91-
const Int Value::minInt = Int(~(UInt(-1) / 2));
92-
const Int Value::maxInt = Int(UInt(-1) / 2);
93-
const UInt Value::maxUInt = UInt(-1);
94-
#if defined(JSON_HAS_INT64)
95-
const Int64 Value::minInt64 = Int64(~(UInt64(-1) / 2));
96-
const Int64 Value::maxInt64 = Int64(UInt64(-1) / 2);
97-
const UInt64 Value::maxUInt64 = UInt64(-1);
98-
// The constant is hard-coded because some compiler have trouble
99-
// converting Value::maxUInt64 to a double correctly (AIX/xlC).
100-
// Assumes that UInt64 is a 64 bits integer.
101-
static const double maxUInt64AsDouble = 18446744073709551615.0;
102-
#endif // defined(JSON_HAS_INT64)
103-
const LargestInt Value::minLargestInt = LargestInt(~(LargestUInt(-1) / 2));
104-
const LargestInt Value::maxLargestInt = LargestInt(LargestUInt(-1) / 2);
105-
const LargestUInt Value::maxLargestUInt = LargestUInt(-1);
106-
107-
const UInt Value::defaultRealPrecision = 17;
10891

10992
#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
11093
template <typename T, typename U>

0 commit comments

Comments
 (0)