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

huangapple go评论60阅读模式
英文:

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

问题

以下是代码的部分翻译:

我有两个类,其中一个是'主'配置,另一个是次要配置,其中包含主配置的某些选项子集。其中一个'主'配置成员是一个用于覆盖主配置选项的次要配置对象的向量(如果存在)。我可以将'主'配置的get方法绑定到我添加到向量中的函数,然后在测试中使用它们来获取这些参数(因此并非每次都测试所有选项,所以我创建一个匹配get方法向量的设置值向量,然后进行比较。)

我希望以类似的方式绑定来自次要配置的get方法,特别是向量中的最后一个(应该是唯一的一个),但我不确定如何做这个,或者是否这样做甚至可行。我已经尝试了一些对我来说有意义的调整,但我开始怀疑这是否是一个好主意,因为我正在创建复杂性。如果有人能告诉我如何实现这一点,无论是通过以一种调用向量中的第一个对象中的所需方法的方式来绑定它,还是通过其他方式,这就是我正在寻找的。我尝试做的事情的一个大大简化的示例如下。

以上是您提供的代码的翻译部分。如果您需要进一步的解释或帮助,请随时提出。

英文:

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;
}

答案1

得分: 3

std::bind 很少是实现某事的最佳或最简单的方式。

使用 lambda 表达式代替。

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

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(); } );

huangapple
  • 本文由 发表于 2023年2月8日 08:40:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定