Skip to content

Commit ad4f8f6

Browse files
authored
Support DEC_TRIVIAL_DEFAULT_CONSTRUCTIBLE (#63)
1 parent 87b31e9 commit ad4f8f6

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

include/decimal.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
// - define DEC_EXTERNAL_LIMITS to define by yourself DEC_MAX_INT32
3737
// - define DEC_NO_CPP11 if your compiler does not support C++11
3838
// - define DEC_ALLOW_SPACESHIP_OPER as 1 if your compiler supports spaceship operator
39+
// - define DEC_TRIVIAL_DEFAULT_CONSTRUCTIBLE as 1 if you want to make default constructor trivial
40+
// use with caution because default constructor will not initialize the object
3941
// - define DEC_TYPE_LEVEL as 0 for strong typing (same precision required for both arguments),
4042
// as 1 for allowing to mix lower or equal precision types
4143
// as 2 for automatic rounding when different precision is mixed
@@ -685,14 +687,23 @@ class decimal {
685687
};
686688

687689
#ifdef DEC_NO_CPP11
688-
decimal() {
689-
init(0);
690-
}
690+
#ifdef DEC_TRIVIAL_DEFAULT_CONSTRUCTIBLE
691+
decimal() {
692+
}
693+
#else
694+
decimal() {
695+
init(0);
696+
}
697+
#endif
691698
decimal(const decimal &src) {
692699
init(src);
693700
}
694701
#else
695-
decimal() noexcept : m_value(0) {}
702+
#ifdef DEC_TRIVIAL_DEFAULT_CONSTRUCTIBLE
703+
decimal() noexcept = default;
704+
#else
705+
decimal() noexcept : m_value(0) {}
706+
#endif
696707
decimal(const decimal &src) = default;
697708
#endif
698709
explicit decimal(uint value) {

tests/decimalTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,24 @@ BOOST_AUTO_TEST_CASE(decimalFloatConstructorHighPrec) {
186186

187187
#ifndef DEC_NO_CPP11
188188
BOOST_AUTO_TEST_CASE(trivialAndNoThrowConstructor) {
189+
#ifdef DEC_TRIVIAL_DEFAULT_CONSTRUCTIBLE
190+
BOOST_CHECK_EQUAL(std::is_trivial<dec::decimal<6>>::value, true);
191+
#else
189192
BOOST_CHECK_EQUAL(std::is_trivial<dec::decimal<6>>::value, false);
193+
#endif
190194

195+
#ifdef DEC_TRIVIAL_DEFAULT_CONSTRUCTIBLE
196+
BOOST_CHECK_EQUAL(std::is_trivially_constructible<dec::decimal<6>>::value, true);
197+
#else
191198
BOOST_CHECK_EQUAL(std::is_trivially_constructible<dec::decimal<6>>::value, false);
199+
#endif
192200
BOOST_CHECK_EQUAL(std::is_nothrow_constructible<dec::decimal<6>>::value, true);
193201

202+
#ifdef DEC_TRIVIAL_DEFAULT_CONSTRUCTIBLE
203+
BOOST_CHECK_EQUAL(std::is_trivially_default_constructible<dec::decimal<6>>::value, true);
204+
#else
194205
BOOST_CHECK_EQUAL(std::is_trivially_default_constructible<dec::decimal<6>>::value, false);
206+
#endif
195207
BOOST_CHECK_EQUAL(std::is_nothrow_default_constructible<dec::decimal<6>>::value, true);
196208

197209
BOOST_CHECK_EQUAL(std::is_trivially_copy_constructible<dec::decimal<6>>::value, true);

0 commit comments

Comments
 (0)