Using overloaded operator (defined in a class) on another method (of the same class) (Eigen usage)

Viewed 68

I have a template

template<typename T>
struct Parameter {
private:
    bool _fixed;
    T _value;
public:
    std::string name;
    const bool* is_fixed = &_fixed;

    Parameter(std::string name, T value) : name(name), _value(value), _fixed(false) {}
    Parameter& operator=(const T& oValue){
        if (_fixed){throw std::runtime_error("Error fixed value..");}
        _value = oValue;
        return *this;
    }
    void fix() { _fixed = true; }
    void unfix() { _fixed = false; }
};

Where the role of Parameter is to basically allow for its value to be 'const-ified'/'fixed' during run time. Heres an example of its usage:

std::string name = "A_Vector";
Eigen::RowVectorXd value = Eigen::RowVectorXd::Ones(1);
Parameter<Eigen::RowVectorXd> param(name, value);
// I can change the value like
param = Eigen::RowVectorXd::Ones(3);
// I can fix the value
param.fix();
// If i were to attempt to redefine it throws runtime error
param = Eigen::RowVectorXd::Ones(5); // Error

However, in order to access Eigen methods i would need to expose the value My work around is to define a .raw() method that returns the value For example, I would like to access the .array() Eigen method and perform computations like param.raw().array()

Obviously this is a loop hole to the 'fixed' attribute as now i can alter the value like:

// I can change the value like
param = Eigen::RowVectorXd::Ones(3);
// I can fix the value
param.fix();
// Loop hole
param.raw() = Eigen::RowVectorXd::Ones(10);

Is it possible to redefine .raw() in a way that the overloaded operator (as defined in Parameter) is used such that

param.raw() = Eigen::RowVectorXd::Ones(10);

Throws an error?

0 Answers
Related