Skip to content

Commit 40dd705

Browse files
authored
Placate Green Hills compiler with ternary (#670)
When the Green Hills compiler encounters an if/else branch whose predicate is known at compile time, it complains about an unreachable statement. Even if the predicate depends on template parameters (i.e., is not a strict constant _across all template instantiations_), the policy triggers on any _individual_ instantiations that contain an unreachable statement. Leaving aside the wisdom of this policy as it pertains to generic programming, the fact is that we're stuck with it and we need to keep the compiler happy. To fix this instance, we simply use a ternary operator. The ternary is just the same as an if/else branch, but it's a single statement. In testing on AV code, this does seem to placate the Green Hills compiler.
1 parent 860ae5f commit 40dd705

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

au/magnitude.hh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,12 +1007,12 @@ struct GetValueResultImplForDefaultCase<T, Magnitude<BPs...>> {
10071007
Exp<BPs>::num,
10081008
static_cast<std::uintmax_t>(Exp<BPs>::den)>(Base<BPs>::value())...});
10091009

1010-
if ((widened_result.outcome != MagRepresentationOutcome::OK) ||
1011-
!safe_to_cast_to<T>(widened_result.value)) {
1012-
return {MagRepresentationOutcome::ERR_CANNOT_FIT};
1013-
} else {
1014-
return {MagRepresentationOutcome::OK, static_cast<T>(widened_result.value)};
1015-
}
1010+
constexpr bool will_fit = widened_result.outcome == MagRepresentationOutcome::OK &&
1011+
safe_to_cast_to<T>(widened_result.value);
1012+
1013+
return will_fit ? MagRepresentationOrError<T>{MagRepresentationOutcome::OK,
1014+
static_cast<T>(widened_result.value)}
1015+
: MagRepresentationOrError<T>{MagRepresentationOutcome::ERR_CANNOT_FIT};
10161016
}
10171017
};
10181018

0 commit comments

Comments
 (0)