lp__ <- lp__ + ifelse(indicator, poisson(...), 0);
template <typename B, typename T>
T if_else(B c, T y_true, T y_false) {
return c ? y_true : y_false;
}
I now realize that this is totally broken.
1. if c is a var, we need c.val() ? ...
not just c ? ...
2. if y_true type doesn't match y_false type,
the template matching will fail. If we generalized
the templates to T1 and T2, then the ternary operator
would fail to match (and we would need to somehow
compute the return type).
I'm going to write the specific cases we care about,
because I can't see how to make templates calculate
the return value properly. I'd very much like this
to work for int values, so we have:
int if_else(int,int,int);
It'd be eaiser if everything could get promoted to
double -- then I could use promote_args. As is, I
can't figure out how to calculate the return type without
writing my own template metaprogram for it or without
a whole lot of code duplication.
First, we need both a regular C++ bool and
a var condition, with the latter calling the former:
if_else(bool c, ...) { ... }
if_else(var v, ...) { return if_else(v.val(),...); }
Then, in terms of the other two args, we need to spell
out seven different implementations:
(C,int,int) -> int
(C,,double,int) -> double
(C,int,double) -> double
(C,double,double) -> double
(C,var,T) -> var
(C,T,var) -> var
(C,var,var) -> var [otherwise former two ambiguous]
So that's 14 different functions for this. And the
API winds up being degeernate because it's only
being defined for int and double.
Am I missing a cleaner way to do this?
- Bob
Here's the definition again:template <typename B, typename T>
T if_else(B c, T y_true, T y_false) {
return c ? y_true : y_false;
}I now realize that this is totally broken.
Am I missing a cleaner way to do this?
template <typename T>
T if_else(bool c, T y_true, T y_false) {
return c * y_true + (!c) * y_false;
}