You're right that the order of the members is unspecified. The definition is inherited from C, which explicitly states it is (emphasis mine):
7.20.6.2 The div, ldiv, and lldiv functions
3 [...] The structures shall contain (in either order) the members quot
(the quotient) and rem
(the remainder), each of which has the same type as the arguments numer
and denom
. [...]
In C, the fact that the order is unspecified doesn't matter, and an example is included specifically regarding div_t
:
6.7.8 Initialization
34 EXAMPLE 10 Structure members can be initialized to nonzero values without depending on their order:
div_t answer = { .quot = 2, .rem = -1 };
Unfortunately, C++ never adopted this syntax.
I'd probably go for simple assignment in a helper function:
div_t make_div_t(int quot, int rem) {
div_t result;
result.quot = quot;
result.rem = rem;
return result;
}
For plain int
values, whether you use initialisation or assignment doesn't really matter, they have the same effect.
Your division by 1
is a valid option as well.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…