Reputation: 137
This is the function I'm trying to simplify:
void BaseCharacter::ClimbLadder(float AxisValue)
{
if (AxisValue > 0.f)
ClimbUpLadder<bIsClimbingLaddersFast>();
else if (AxisValue < 0.f)
ClimbDownLadder<bIsClimbingLaddersFast>();
}
With bIsClimbingLaddersFast being a Boolean member variable. Now, the code doesn't compile because the value of the Boolean needs to be known in compile time. This is the trivial solution:
void BaseCharacter::ClimbLadder(float AxisValue)
{
if (bIsClimbingLaddersFast)
{
if (AxisValue > 0.f)
ClimbUpLadder<true>();
else if (AxisValue < 0.f)
ClimbDownLadder<true>();
}
else
{
if (AxisValue > 0.f)
ClimbUpLadder<false>();
else if (AxisValue < 0.f)
ClimbDownLadder<false>();
}
}
But I'm wondering if there is a cleaner way to write this, one without the duplicate if-else statements. C++14 is being used.
Upvotes: 0
Views: 90
Reputation: 16089
A possible (totally untested code) solution.
template<bool climb>
void BaseCharacter::ClimbLadder(float AxisValue) {
if (AxisValue > 0.f)
ClimbUpLadder<climb>();
else if (AxisValue < 0.f)
ClimbDownLadder<climb>();
}
void BaseCharacter::ClimbLadder(float AxisValue) {
if (bIsClimbingLaddersFast)
ClimbLadder<true>(AxisValue);
else
ClimbLadder<false>(AxisValue);
}
Upvotes: 1