c++vectorstdvectorstd-functionstdbind

Is it possible to bind a function from a vector of objects to a std::function, or how would you access a function from a vector of objects this way


I have a two classes, one of which is a 'main' configuration, the other is a secondary configuration with a subset of some options from the main configuration. One of the 'main' configuration members is a vector of secondary configuration objects used to override the main configuration options if present. I can bind the 'main' configurations get methods to functions which I add to a vector and use to get those parameters in testing (not all options are tested every time, so I create a vector of set values which matches the vector of get methods which I compare.)

I would like to similarly bind the get methods from the secondary configurations, specifically the last (should be only) one in the vector, but I am unsure how to do this or if it is even possible. I have tried a number of tweaks that make sense to me, however I am starting to doubt if this would even be a good idea with the amount of convolution I am creating. If anyone could point me to how I would accomplish this, either by binding it in such a way that it calls the desired method from the first object in the vector or another way, that is what I am looking for. A heavily paired down example of what I am attempting to do is below.

#include <iostream>
#include <vector>
#include <functional>

using namespace std;

class secConfig{
private:
    double data;
public:
    secConfig(double);
    double get_data();
};
secConfig::secConfig(double indata){
    data = indata;
}
double secConfig::get_data(){
    return data;
}

class config{
private:
    std::vector<secConfig> secConfigs; 
    double normalData;
public:
    config(double, vector<secConfig>);
    double getData();
    vector<secConfig> getVecData();
};
config::config(double inData, vector<secConfig> inVec){
    normalData = inData;
    secConfigs = inVec;
}
double config::getData(){
    return normalData;
}
vector<secConfig> config::getVecData(){
    return secConfigs;
}

int main(int argc, char const *argv[])
{
    secConfig initial(66.6);
    vector<secConfig> inConfigs;
    inConfigs.push_back(initial);
    cout << "Inconfig raw data: " << initial.get_data() << endl;
    cout << "Inconfig vector data: " << inConfigs.back().get_data() << endl;

    config mainConfig(55.5, inConfigs);
    cout << "Main config raw data: " << mainConfig.getData() << endl;
    cout << "Main config internal vec data: " << mainConfig.getVecData().back().get_data() << endl;

    vector<function<double()>> getvals;
    getvals.push_back(bind(&config::getData, &mainConfig));
    cout << "Main config bound data: " << getvals[0]() << endl;
    // Something like: getvals.push_back(bind(&config::getVecData::back::get_data, &mainConfig));
    // So I can: cout << "Secondary (vectorized) config data : " << getvals[1]() << endl;

    return 0;
}


Solution

  • std::bind is rarely the best or easiest way to achieve something.

    Use a lambda instead.

    getvals.push_back( [&]{ return mainConfig.getVecData().back().get_data(); } );