dimanche 30 septembre 2018

type deduction with auto &

Today I wrote some code dealing with binary tree. Later I noticed a bug in the code:

void find(TreeNode* root, int target) {
    stack<int> myStack;
    while(!myStack.empty()){
        auto& top = myStack.top(); // here I used auto& by mistake
        ...
    }
}

However, I am confused with auto& top = myStack.top();. After the type deduction, what is the type for top? Is it TreeNode & or TreeNode* &?

How about if I used auto* top = myStack.top()?

Aucun commentaire:

Enregistrer un commentaire