mardi 2 mai 2017

need to return the same type as this from c++ template function

I want a template function to return the same type as this. It is easy to get the types of arguments in parens, but I do not see how to get the type of the object whose method is called.

struct ENode {
    ENode* mnext;
    ENode(ENode* n):mnext(n){}
    template<typename T>T* next(T* unused) { return (T*)mnext; }
    template<typename T>T* xnext() { return (T*)mnext; }
};

struct TNode : public ENode {
    int val;
    TNode(int v, TNode* n):val(v),ENode(n){}
};

void printTN(TNode* n) {
    while(n) {
    printf("%i -> ", n->val);
//  n=n->next(n); // ok, but n is unused
//  n=n->xnext(); // error: template argument deduction/substitution failed:  couldn't deduce template parameter ‘T’
//  n=n->xnext<std::remove_reference<decltype(*n)>::type>(); // works, but is very ugly
    n=n->xnext<TNode>(); // works, but is ugly
    }
    printf("nil\n");
}

How do I have n->next() return the same type as n?

Aucun commentaire:

Enregistrer un commentaire