问题
I have some templates like the ones below that I can use to define simple expressions e.g.
Expr<constant,int,int,1,1>
= 2
Expr<sub, Expr<constant,int,int,1,1>, Expr<constant,int,int,2,0>, 1, 1>
= x - 2.
I want to define a meta-function that takes an Expr and returns another Expr that is a modified version of the one passed as input. The output will be based on the template arguments of the input so I guess I have to define multiple function templates that specialize different inputs. Eventually my goal is to be able to differentiate Expr's.
// the types of expressions (+,-,*, etc.)
enum ExprType { mul, divide, add, sub, constant};
// constant
template <ExprType eType, class Left, class Right, int coeff, int power> struct Expr {
static double eval(double x){
return coeff * std::pow(x, power);
}
};
//sub
template <class Left, class Right, int coeff, int power> struct Expr<sub, Left, Right, coeff, power> {
static double eval(double x){
return coeff * std::pow(Left::eval(x) - Right::eval(x), power);
}
};
// add
template <class Left, class Right, int coeff, int power> struct Expr<add, Left, Right, coeff, power> {
static double eval(double x){
return coeff * std::pow(Left::eval(x) + Right::eval(x), power);
}
};
However, I am having trouble wrapping my head around the function definition. So far I have:
template <template <ExprType eType, class Left, class Right, int coeff, int power> class E> struct ExprDerivative {
static E derivative(E e){
return e;
}
};
Am I going in the right direction? How do I define a meta-function over templates?
回答1:
You want a type trait, which is a function with types as arguments. A type trait's definition looks nothing like a function on values (they're essentially written in a functional programming style, with "equations"), but they're called as you'd expect (func<args>
).
template<typename Differentiand> struct derivative;
// shorthand for calling the function: derivative_t<expr...>
template<typename Differentiand>
using derivative_t = typename derivative<Differentiand>::result;
// every "equation" is a specialization of derivative for a certain set of Exprs that defines the result as a member type
template<typename L, typename R, int coeff, int power>
struct derivative<Expr<constant, L, R, coeff, power>> { using result = Expr<constant, L, R, coeff*power, power - 1> };
// etc
However, I am worried about how you've written the Expr
type in the first place. constant
s are not constants; they're expressions of the form cx^n
. Also, they have extraneous left and right operands. It would be better to do this
struct variable {
static constexpr double eval(double x) { return x; }
};
template<int Value>
struct constant {
static constexpr double eval(double x) { return Value; }
};
template<typename Left, typename Right>
struct addition {
static constexpr double eval(double x) { return Left::eval(x) + Right::eval(x); }
};
template<typename Left, typename Right>
struct multiplication {
static constexpr double eval(double x) { return Left::eval(x) * Right::eval(x); }
};
template<typename Base, int Power>
struct exponentiation {
static double eval(double x) { return std::pow(Base::eval(x), Power); }
};
// no need to add these as "primitives"
template<typename Left, typename Right>
using subtraction = addition<Left, multiplication<constant<-1>, Right>>;
template<typename Left, typename Right>
using division = multiplication<Left, exponentiation<Right, -1>>;
The results of differentiation do end up a little less simplified, but you can write another function to clean up after:
template<>
struct derivative<variable> { using result = constant<1>; };
template<int Value>
struct derivative<constant<Value>> { using result = constant<0>; };
template<typename L, typename R>
struct derivative<addition<L, R>> { using result = addition<derivative_t<L>, derivative_t<R>>; };
template<typename L, typename R>
struct derivative<multiplication<L, R>> { using result = addition<multiplication<derivative_t<L>, R>, multiplication<L, derivative_t<R>>>; };
template<typename B, int N>
struct derivative<exponentiation<B, N>> { using result = multiplication<multiplication<constant<N>, exponentiation<B, N - 1>>, derivative_t<B>>; };
E.g.
int main() {
// y = (x^2 + 1)/x
// dy/dx = 1 - x^-2
// dy/dx(x = 2) = 1 - 1/4 = 0.75
std::cout << derivative_t<division<addition<exponentiation<variable, 2>, constant<1>>, variable>>::eval(2) << "\n";
}
回答2:
Not sure to understand what do you want... but sure you can't pass a template-template argument as a function argument.
Seems to me that your ExprDerivative()
functional can be written, as template function that permit to deduce the template-template and the template parameters from the e
argument, as follows
template <template <ExprType, typename, typename, int, int> class E,
ExprType eType, typename Left, typename Right,
int coeff, int power>
auto ExprDerivative (E<eType, Left, Right, coeff, power> e)
{ return e; }
Observe that, this way, the argument e
is of type E<eType, Left, Right, coeff, power>
, not of type E
(that isn't a type).
You can use it, by example, as follows
Expr<constant, int, int, 1, 1> e0;
auto e1 = ExprDerivative(e0);
来源:https://stackoverflow.com/questions/60365244/c-meta-function-over-templates