C++ Lambda返回空字符串

C++ Lambda返回空字符串,c++,string,c++11,lambda,C++,String,C++11,Lambda,我有学费 class Fee { private: std::string _code; int _value; std::string _description_EN; std::string _description_UA; std::vector<std::function<std::string()>> get_description; public: //CONSTRUCTORS Fee(int value

我有学费

class Fee
{
private:
    std::string _code;
    int _value;
    std::string _description_EN;
    std::string _description_UA;
    std::vector<std::function<std::string()>> get_description;
public:
    //CONSTRUCTORS
    Fee(int value, std::string_view code, std::string_view description_EN, std::string_view description_UA);
    Fee(const std::string _csv_line, const char separator);
    Fee(const Fee &) = delete;
    Fee(Fee &&) = default;
    //OPERATORS
    Fee &operator=(const Fee &) = delete;
    Fee &operator=(Fee &&) = default;
    Fee &operator++() = delete;
    Fee &operator++(int) = delete;
    Fee &operator--() = delete;
    Fee &operator--(int) = delete;
    Fee &operator+(const Fee &other) = delete;
    Fee &operator-(const Fee &other) = delete;
    Fee &operator+=(const Fee &other) = delete;
    Fee &operator-=(const Fee &other) = delete;
    Fee &operator/(const Fee &other) = delete;
    Fee &operator*(const Fee &other) = delete;
    Fee &operator/=(const Fee &other) = delete;
    Fee &operator*=(const Fee &other) = delete;
    Fee &operator%(const Fee &other) = delete;
    Fee &operator%=(const Fee &other) = delete;
    //SETTERS
    void set_new_value(int value);
    //GETTERS
    std::string code();
    int value();
    std::string description(Language language = Language::EN);
    //FUNCTIONS

    //DESTRUCTOR
    ~Fee() = default;
};
学费
{
私人:
std::字符串_代码;
int_值;
标准::字符串_描述_EN;
std::string _description_UA;
std::向量get_描述;
公众:
//建设者
费用(int值、std::string\u视图代码、std::string\u视图描述\u EN、std::string\u视图描述\u UA);
费用(常量标准::字符串_csv_行,常量字符分隔符);
费用(施工费&)=删除;
费用(费用&&)=默认值;
//操作员
费用和运营商=(施工费用和)=删除;
费用和运营商=(费用和运营商)=默认值;
费用和运营商++()=删除;
费用和操作员++(int)=删除;
费用和运营商--()=删除;
费用和运营商--(int)=删除;
费用和运营商+(施工费用和其他)=删除;
费用和运营商-(施工费用和其他)=删除;
费用和运营商+=(施工费用和其他)=删除;
费用和运营商-=(施工费用和其他)=删除;
费用和运营商/(施工费用和其他)=删除;
费用和运营商*(施工费用和其他)=删除;
费用和运营商/=(施工费用和其他)=删除;
费用和运营商*=(施工费用和其他)=删除;
费用和运营商%(施工费用和其他)=删除;
费用和运营商%=(施工费用和其他)=删除;
//二传手
无效设置新值(int值);
//吸气剂
std::字符串代码();
int值();
std::字符串描述(语言=语言::EN);
//功能
//析构函数
~Fee()=默认值;
};
以及存储费用地图的班级收费表

class FeeList
{
private:
    std::map<std::string, Fee> _fee_list;
    FeeList() = default;
public:
    static FeeList &fee_list();
    //CONSTRUCTORS
    FeeList(const FeeList &) = delete;
    FeeList(FeeList &&) = delete;
    //OPERATORS
    FeeList &operator=(const FeeList &) = delete;
    FeeList &operator=(FeeList &&) = delete;
    //SETTERS

    //GETTERS
    Fee &fee(const std::string &code);
    //FUNCTIONS
    void addFee(Fee &fee);
    void from_csv_file(const std::string &inv_file, const std::string &um_file, const std::string &id_file, const std::string &tr_file, const char separator);
    //DESTRUCTOR
    ~FeeList() = default;

};
class-FeeList
{
私人:
标准::地图费用清单;
FeeList()=默认值;
公众:
静态费用清单和费用清单();
//建设者
FeeList(const FeeList&)=删除;
FeeList(FeeList&&)=删除;
//操作员
FeeList&运算符=(const FeeList&)=删除;
FeeList&运算符=(FeeList&&)=删除;
//二传手
//吸气剂
费用和费用(const std::字符串和代码);
//功能
无效附加费(费用和费用);
从_csv_文件作废(const std::string和inv_文件、const std::string和um_文件、const std::string和id_文件、const std::string和tr_文件、const char分隔符);
//析构函数
~FeeList()=默认值;
};
class Fee的构造函数有以下几行代码,它们由lambdas填充“get_description”向量

get_description.resize(2);
    get_description[static_cast<size_t>(fee::Language::EN)] = [this]()->std::string{return _description_EN;};
    get_description[static_cast<size_t>(fee::Language::UA)] = [this]()->std::string{return _description_UA;};
get_description.resize(2);
get_description[static_cast(fee::Language::EN)]=[this]()->std::string{return _description_EN;};
get_description[static_cast(fee::Language::UA)]=[this]()->std::string{return _description_UA;};
这些lambda由函数“description(fee::Language::)”调用,该函数应返回描述语言。 实施是相当直接的

std::string fee::Fee::description(Language language)
{
    return get_description[static_cast<size_t>(language)]();
}
std::字符串费用::费用::说明(语言)
{
返回get_description[静态_cast(语言)]();
}
问题是空字符串是从lambda返回的。 我已经创建了一个简单的类来测试这种方法,它按照预期工作。我不知道问题出在哪里。我正在获取其他变量(代码和值)的值,以便正确存储对象

编辑:这里有一个到coliru.stacked-crooked.com的链接,其中粘贴了我的代码并处理了提到的问题(int值;和字符串代码;是Ok字符串描述;是空的)
使用Coliru命令行也可以找到该文件:cat/Archive2/bc/56eb53400bd1af/main.cpp

隐式实例化的
Fee
get\u description
中将构造函数副本移动到lambda上,但它不会到达这些lambda内部并更新它们捕获的指针。它们仍然指向原始的
Fee
对象,该对象已从中移动。然后,您的程序通过访问从对象移动的对象来显示未指定的行为


与其拥有两个单独的字符串变量和一个lambda向量,不如只拥有一个字符串向量?

隐式实例化的
Fee
移动构造函数在
get\u description
中复制lambda,但它不会到达这些lambda内部并更新它们捕获的指针。它们仍然指向原始的
Fee
对象,该对象已从中移动。然后,您的程序通过访问从对象移动的对象来显示未指定的行为


与其拥有两个单独的字符串变量和一个lambda向量,不如只拥有一个字符串向量?

可能由于lambda中的
[此]
而挂起引用。如果[1]费用被创建,[2]费用被移动到
FeeList
的地图中,您认为该
指针会发生什么情况。[3] lambda是根据map的费用调用的。很难说没有看到MCVE。嗨,Mitya,欢迎来到Stack Overflow。请提供一个(我们可以粘贴到的东西,例如,我们可以看到问题所在)。更好的设计是让
std::map
shared\u ptr
,这样你就不必担心对象四处移动。我考虑的是unique\u ptr。包括。但是首先要处理主要问题。可能由于lambda中的
[这]
而挂起引用。如果[1]费用被创建,[2]费用被移动到
FeeList
的地图中,您认为该
指针会发生什么情况。[3] lambda是根据map的费用调用的。很难说没有看到MCVE。嗨,Mitya,欢迎来到Stack Overflow。请提供一个(我们可以粘贴到的东西,例如,我们可以看到问题所在)。更好的设计是让
std::map
shared\u ptr
,这样你就不必担心对象四处移动。我考虑的是unique\u ptr。包括。但是首先我想处理主要问题。谢谢你的澄清。感谢您对字符串向量的好主意。也许您可以建议如何处理lambdas的这种行为。对于未来的案例。我们如何使用用户定义的移动构造函数进入堪萨斯州。解决方案取决于您在未来的情况下使用lambdas的目的。在本例中,它们完全没有必要。但是如果你因为某种原因