dimanche 28 août 2016

overloaded operator with std::function parameter

I'm working on Multi-type map holder. It works with all primitive types and also with structs e.g. Point. However, if I want to add std::function as another supported type (used for callbacks) then the compiler complains:

MT.cpp:426:15: No viable overloaded '='

MT.h:31:7: Candidate function (the implicit copy assignment operator) not viable: no known conversion from '(lambda at MT.cpp:426:17)' to 'const sharkLib::MT' for 1st argument

MT.h:31:7: Candidate function (the implicit move assignment operator) not viable: no known conversion from '(lambda at MT.cpp:426:17)' to 'sharkLib::MT' for 1st argument

I don't actually overload = operator but instead overload [] with dedicated constructor per supported type.

.h

protected: 
    map<string,MT> valueMap;

public:
    MT (int value);
    MT (std::function<void(Ref*)> ccb);
    virtual MT& operator[] (const char* key);

.cpp

MT::MT (int value)
{
    this->type = ValueType::intValue;
    this->value.int_ = value;
}

MT::MT (std::function<void(Ref*)> value)
{
    this->type = ValueType::ccbValue;
    this->value.ccb_ = value;
}

MT& MT::operator[] (const char* key)
{
    return this->valueMap[key];
}

usage

MT mt;

mt["int"] = 1;
mt["ccb"] = [](Ref *){ CCLOG("Pressed"); };

This last line is the one with error.

Aucun commentaire:

Enregistrer un commentaire