Getting started with boost mpl with vector and push_back
Asked Answered
D

0

0

I've been scratching my head for far too long, I'm finding using MPL very difficult to get around and hoping somebody can get me started. Here is some partial code from a class I am developing which does not use MPL. I eventually want to implement this class all at compile time. This code probably won't make sense but I don't want all the solution in MPL - hopefully I can achieve that myself (see below for particular help).

class define_cell_type{
public:
    define_cell_type () = default;
    define_cell_type (const std::string& name_of_cell) : 
            cName {name_of_cell} {};

    double& add_variable (const std::string& Name,
            const double& init_value,
            VARIABLE declr_type = VARIABLE::STATIC_V)
    {
       vInit_val.push_back(init_value);
       vData.push_back( std::make_tuple(Name) );
            return vInit_val.back();
    };

private:
    std::string cName;
    std::vector<double> vInit_val;
    std::vector<variable_tuple> vData;
};

To get started, how do I do push_back on an mpl::vector of type double? here is an example I want to begin with similar to the function in the class I'm developing.

std::vector<double> state;
double& add_variable ( const double& init_val)
{
    state.push_back(init_val);
    return state.back();
} 

int main() {
    auto var1 = add_variable (12.2);
    auto var2 = add_variable (1.2);
    auto var2 = add_variable (6.4);
}

All I can get is something like this

typedef mpl::vector<double> state;
typedef mpl::push_back<state,double>::type types;

I would be grateful if somebody got me started, to produce a compile time vector with values pushing back the example above

Dulciana answered 18/3, 2013 at 6:47 Comment(7)
It looks like you want to store actual, run-time determined values in your vector, is that true? Because in that case either using tuples or boost.fusion would be a better solution. Mpl vector is a list of types, not values. You can create types to represent integral constants, but encoding a float in a type is not going to be easy.Gelid
@Gelid Thanks for your comment, all these values are known at compile-timeDulciana
You can't store values as doubles, only integral constants(such as int, bool, and function pointers). You could devise a way to store the numbers as integral constants(perhaps ratio of integers). Or you could just store the values in an array.Fulgurate
@Paul That would be be fine for my program, tuple or c++11 array, though there are variable number of these. Is it possible to allow automatic generation of mpl/fusion vector of these types as a function similar to push_back? I can think of right now is fusion::make_vector(var1, var2,...,varN) where varx is some std::array or constexpr tuple storage of some value of type double. Note that N can vary at compile time. But this is intrusive, since I'll have to change this part of code everytime number of vars changeDulciana
@Dulciana Mpl/fusion/tuple/array are all immutable data structures. Using push_back just creates a new type, with the new value at the end. It seems you want to register these values at different places in your code. I dont know exactly what you want to achieve with these values, but you may need to register them at run-time, instead.Fulgurate
@paul in my program I reference these values (user defined) in order to know what these variables are for. These values are stored in a container initially since I will want to do collective operations on all values (i.e. write to file iterating through all container) or partial operations (depending on variable type in class). want to represent these operations more generically for the compiler to see instead of nested loops and if statements etc which turned out to be costly which prob can be optimized. In any event I'll re-edit the question for more clarity with updated code later.Dulciana
@Dulciana Yea, definitely, more detail and clarity will help.Fulgurate

© 2022 - 2024 — McMap. All rights reserved.