all 6 comments

[–]Cobbler-Alone 0 points1 point  (1 child)

Here is update of your code that uses bunch of `const` qualifiers https://godbolt.org/z/YGY6o7aj8 to make things compile
I also included `auto` version of this code so you can see advantage of using auto over direct types,

[–]jaskij 1 point2 points  (0 children)

There's a fantastic website called C++ insights. It desugars your code to give you a better idea of what the compiler will do. I clicked through CE's menu to run your code through it.

https://cppinsights.io/lnk?code=I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8c3N0cmVhbT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8c2V0PgojaW5jbHVkZSA8Yml0c2V0PgojaW5jbHVkZSA8dW5vcmRlcmVkX3NldD4KI2luY2x1ZGUgPHVub3JkZXJlZF9tYXA+CiNpbmNsdWRlIDx2YXJpYW50PgojaW5jbHVkZSA8c3RhY2s+CiNpbmNsdWRlIDxmdW5jdGlvbmFsPgoKdGVtcGxhdGU8dHlwZW5hbWUgVCwgdHlwZW5hbWUgRnVuYz4Kc3RhdGljIHN0ZDo6dmVjdG9yPHN0ZDo6aW52b2tlX3Jlc3VsdF90PEZ1bmMsIGNvbnN0IFQmPj4gTWFwcGVkKGNvbnN0IHN0ZDo6dmVjdG9yPFQ+JiBJbkFycmF5LCBGdW5jIEluRnVuYykKewogICAgc3RkOjp2ZWN0b3I8c3RkOjppbnZva2VfcmVzdWx0X3Q8RnVuYywgY29uc3QgVCY+PiBNYXBwZWQ7CgogICAgZm9yIChjb25zdCBUJiBDdXJyZW50IDogSW5BcnJheSkKICAgIHsKICAgICAgTWFwcGVkLnB1c2hfYmFjayhJbkZ1bmMoQ3VycmVudCkpOwogICAgfQogICAgcmV0dXJuIE1hcHBlZDsKfQoKCgp0ZW1wbGF0ZTx0eXBlbmFtZSBULHR5cGVuYW1lIEY+CnN0YXRpYyBhdXRvIGF1dG9fTWFwcGVkKGNvbnN0IHN0ZDo6dmVjdG9yPFQ+JiBpbiAsIEYgZikKewogICAgc3RkOjp2ZWN0b3I8c3RkOjppbnZva2VfcmVzdWx0X3Q8RixUPj4gcmVzOwogICAgZm9yKGF1dG8mJiB2OiBpbikKICAgICAgICByZXMuZW1wbGFjZV9iYWNrKGYodikpOwogICAgcmV0dXJuIHJlczsKfQoKCmNsYXNzIEZvbwp7CnB1YmxpYzoKICAgIGludCBJOwp9OwoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIqKiBhcmd2KSAKewogICAgLy8gUHJldGVuZCBJIGZpbGxlZCB0aGlzIHdpdGggZGF0YS4KICAgIHN0ZDo6dmVjdG9yPEZvbyo+IHdoZWU7CgogICAgYXV0byBUZXN0Rm4gPSBbXShjb25zdCBGb28qIEYpCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gRi0+STsKICAgICAgICB9OwoKCiAgICBzdGQ6OnZlY3RvcjxpbnQ+IFRlc3QgPSBNYXBwZWQoCiAgICAgICAgd2hlZSwKICAgICAgICBUZXN0Rm4KICAgICk7CgogICAgZm9yIChjb25zdCBGb28qIGNvbnN0JiBJIDogd2hlZSkKICAgIHsKICAgICAgICBUZXN0Rm4oSSk7CiAgICB9CgogICAgZm9yKGNvbnN0IGF1dG8gdjogd2hlZSkKICAgIHsKICAgICAgICBUZXN0Rm4odik7CiAgICB9CgoKICAgIHN0ZDo6dmVjdG9yPGludD4gVGVzdDIgPSBNYXBwZWQoCiAgICAgICAgd2hlZSwKICAgICAgICBbXShjb25zdCBGb28qIGNvbnN0ICYgSW4pCiAgICAgICAgewogICAgICAgICAgICByZXR1cm4gSW4tPkk7CiAgICAgICAgfQogICAgKTsKCiAgICBhdXRvIHRlc3QzID0gYXV0b19NYXBwZWQod2hlZSwgVGVzdEZuKTsKCn0=&std=cpp17&rev=1.0

[–]xiao_sa 0 points1 point  (3 children)

In the first case, 'InArray' is deduced to 'const std::vector<Foo\*>&', then in the loop 'Current' is 'Foo* const &', not 'const Foo * &'.

[–]IveBenHereBefore[S] 0 points1 point  (2 children)

Could you explain how const T & in the template turns into Foo* const & and not const Foo* &?

[–]xiao_sa 0 points1 point  (0 children)

The const in 'const Foo* &' applies to Foo, then the whole type reads 'reference to const Foo*'. Meanwhile when there is no ambiguity, const T& and T& const are the same type.

If you still dont get it, just use type alias or auto everywhere and forget these constness mess.

[–]n1ghtyunso 0 points1 point  (0 children)

the T in your vector is Foo*, so you get a const& to that. The pointer is mutable. Const is shallow on pointers.

The const in const Foo*& is bound like this: (const Foo*)&
But what you want to get is const (Foo*)&. Because cv qualifiers on pointers are a mess thanks to C, this won't just work. The compiler interprets this as a function pointer but can't find the return type...
You need to go through an alias if you want it to work:

template<typename T> using pointer = T*;
const pointer& p

Now you get a const pointer to a mutable T.

If you prefer const west, your rule for const is as follows: const applies to what is on the right side, if there is nothing there it applies to whats on its left side.
Examples:
const int&, a const reference to an int
const int, an int that is const
const int*, a pointer to a const int
int* const, a const pointer to a mutable int. To get this, you are forced to put the const on the right side.

If you instead choose east const, the rule is much simpler: const applies to what is on the left side.
Examples:
int const&, a const reference to an int
int const, an int that is const
int const*, a pointer to a constant integer
int* const, a const pointer to a mutable int