mercredi 5 décembre 2018

Adding another parameter to the class renders code uncompilable

I created a class extending a map-of-handlers collection, that does some tasks, according to reflexive arguments (it's an attempt to make a reflection for c++). However when I parameterized it to be not bound to a specific implementation, the compiler refused to work, showing strange error. Long things short, this compiles:

using namespace std;

template<class T>
class Blah {
public:
    using ctx_t = map<T, string>;

private:
    map<string, function<void(ctx_t&)>> handlers;

protected:
    void registerHandler(const string& name, function<void(ctx_t&)> fn) {
        handlers[name] = fn;
    }
};

class BlahDescendant : public Blah<string> {
public:
    BlahDescendant() {
        registerHandler("ala", [](ctx_t& m) {cout << m["ala"]; });
    }
};

int main() {
    BlahDescendant desc;
    return 0;
}

But when parameterized, it refuses to compile, saying that

testfield.cpp:33:35: error: 'ctx_t' has not been declared

.

using namespace std;

template<class T, class X>
class Blah {
public:
    using ctx_t = map<T, X>;

private:
    map<string, function<void(ctx_t&)>> handlers;

protected:
    void registerHandler(const string& name, function<void(ctx_t&)> fn) {
        handlers[name] = fn;
    }
};

template<class X>
class BlahDescendant : public Blah<string, X> {
public:
    BlahDescendant() {
        registerHandler("ala", [](ctx_t& m) {cout << m["ala"]; });
    }
};

int main() {
    BlahDescendant<string> desc;
    return 0;
}

When trying to give map in lambdas there is different error reported:

error: 'registerHandler' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
     registerHandler("ala", [](map<string,X>& m) {cout << m["ala"]; });

Can anybody help?

Aucun commentaire:

Enregistrer un commentaire