Search code examples
c++tuplesc++17variadic-templates

Creating a simple expression class with parameter packs and tuples


I'm trying to make an expression class that takes classes which derive from "DMGType" and can sum them together, for example:

 CritDamage+4*AllDamage+MeleeDamage

And then calculate its value using a "build" parameter, e.g. if build is "Mage" = 0 in programming terms the result should be:

CritDamage(0)+4*AllDamage(0)+MeleeDamage(0) = 0.5+4*1+0

Here is my attempt:

#include<iostream>
#include<tuple>
#include<cstddef>
template <typename ValueType>
class DMGType {
public:
    ValueType coeff=0;
    virtual ValueType getExpo(std::size_t build) = 0;
    DMGType<ValueType>& sum(DMGType<ValueType> const& dmg) {
        this->coeff+=dmg.coeff;
        return this;
    }
};

template<typename ValueType, class... DMGTypesDerived>
class Expression {
public:
    std::tuple<DMGTypesDerived...> parameters;
    void sum(DMGType<ValueType> const& dmg) {
        std::get<dmg>(parameters).sum(dmg);
    }
    ValueType result{0};
        //desired result: return std::get<DMGTypes1>(parameters).getExpo(build) + ... + std::get<DMGTypesLast>.getExpo(build);
        result+=std::get<DMGTypesDerived>(parameters).getExpo(build) + ...;
        return result;
};

class CritDamage: public DMGType<double> {
public:
    double getExpo(std::size_t build) {
        return build+1;
    }
};

int main() {
    Expression<double, CritDamage> test;
    CritDamage crit;
    crit.coeff=1;
    test.sum(crit);
    std::cout<<test.getExpo(0);
}

The errors I'm getting:

prog.cc:24:69: error: expected ';' before '+' token
   24 |         result+=std::get<DMGTypesDerived>(parameters).getExpo(build) + ...;
      |                                                                     ^~
      |                                                                     ;
prog.cc:24:15: error: parameter packs not expanded with '...':
   24 |         result+=std::get<DMGTypesDerived>(parameters).getExpo(build) + ...;
prog.cc:24:15: note:         'DMGTypesDerived'

I'm open to lambda functions, std::apply, invoke, forward expansion/folding, but everything I've tried so far gave me some errors as I don't fully understand these concepts.

I'm also aware of problems that will arise from the Expression::sum function which won't handle polymorphism, and I'd be thankful for help on that matter too.

Solution

#include<iostream>
#include<tuple>
#include<cstddef>
template <typename ValueType>
class DMGType {
public:
    DMGType(ValueType v) 
    :coeff(v) {};
    DMGType() 
    :coeff(0) {};
    ValueType coeff=0;
    virtual ValueType getExpo(std::size_t build) = 0;
    void sum(DMGType<ValueType> const& dmg) {
        this->coeff+=dmg.coeff;
    }
};

template<typename ValueType, class... DMGTypesDerived>
class Expression {
public:
    std::tuple<DMGTypesDerived...> parameters;
    template<class DerivedClass>
    void sum(DerivedClass const& dmg) {
        std::get<DerivedClass>(parameters).sum(dmg);
    }
    ValueType getExpo(std::size_t build) {
        return (std::get<DMGTypesDerived>(parameters).getExpo(build) + ...);
    }
};

class CritDamage: public DMGType<double> {
public:
    double coeff=0;
    CritDamage(double v) :coeff(v) {}
    CritDamage() :coeff(0) {}
    double getExpo(std::size_t build) {
        return build+1;
    }
};

class MeleeDamage: public DMGType<double> {
public:
    double coeff=0;
    MeleeDamage(double v) :coeff(v) {}
    MeleeDamage() :coeff(0) {}
    double getExpo(std::size_t build) {
        return build*2+1;
    }
};

int main() {
    Expression<double, CritDamage, MeleeDamage> test;
    CritDamage crit(1);
    test.sum(crit);
    MeleeDamage melee(2);
    melee.sum(crit);
    std::cout<<test.getExpo(1);
}

Solution

  • You’re missing some parentheses on the fold expression. Try this:

    ValueType getExpo(std::size_t build) {
        ValueType result{0};
        result += (std::get<DMGTypesDerived>(parameters).getExpo(build) + ...);
        return result;
    }
    

    In fact, you could skip result and just do this:

    ValueType getExpo(std::size_t build) {
        return (std::get<DMGTypesDerived>(parameters).getExpo(build) + ...);
    }