vendredi 27 septembre 2019

C++ Lambda returns empty string

I have a class Fee.

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

And class FeeList which stores map of Fees

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;

};

Constructor of class Fee has the following lines of code which fill up the "get_description" vector by lambdas

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

Those lambdas are invoked by function "description(fee::Language::)" which should return description in appropriate language. Implementation is quite strait forward

std::string fee::Fee::description(Language language)
{
    return get_description[static_cast<size_t>(language)]();
}

The problem is that the empty string is returned from lambda. I have created simple class to test such approach and it worked as expected. I can't figure out were is the problem. I'm getting values of other variables (code and value) so object is stored correctly.

Aucun commentaire:

Enregistrer un commentaire