1

I have a multi-index defined such that I can do:

<type?> test() {
    myTable t(_self, _self.value);
    auto r = t.find(...);
    return r;
}

but how do I return the iterator? what is the type I need to declare for my method?

according to https://eosio.github.io/eosio.cdt/1.6.0/group__multiindex.html#function-find the return type is const_iterator but the compiler complains that no type of that name exists, and the documentation says nothing of where I might find the type

help?

2 Answers 2

1

The return type is your table type as a namespace for the const_iterator, like this: myTable::const_iterator.

However, it is probably best to just return type auto. That way you can't go wrong.

4
  • I did try the eosio namespace as you recommended but that did not work. auto was the right answer but it I could not return r. I needed to return &*r
    – ekkis
    Commented Jul 8, 2019 at 22:34
  • What was the error message when you tried to just return r? Commented Jul 8, 2019 at 22:41
  • @ekkis I updated my answer for the case where you don't want to use auto Commented Jul 9, 2019 at 21:42
  • I did try myTable:: but the compiler kept complaining the symbol didn't exist. I think I needed to scope it: token::myTable::const_iterator
    – ekkis
    Commented Jul 9, 2019 at 22:58
2

You can return auto. Your code example would then look like this:

// attention, undefined behaviour
auto test() {
    myTable t{_self, _self.value};
    return t.find(...);
}

In your code example, however, the variable t is stack allocated and goes out of scope at the end of the function. That results in undefined behaviour since you are now returning an iterator which points to an object whose memory has been deallocated.

If you still really want to do it, you need to allocate on the heap. Since inside a smart contract, we don't need to take care of cleanup up memory, probably the simplest way of doing it would be like this:

auto test() {
    auto *t = new myTable{_self, _self.value};
    return t->find(...);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.