I want to use range-based for
to iterate over the unicode code points in a UTF8 encoded std::string
. I have defined my own begin
and end
in the global namespace but the begin
and end
in the std
namespace are being preferred (i.e. those found by ADL). Is there any way to prefer my own functions?
Example:
const char* begin(const std::string& s) {
std::cout << "BEGIN";
return s.data();
}
const char* end(const std::string& s) {
std::cout << "END";
return s.data() + s.length();
}
int main() {
std::string s = "asdf";
for (char c : s)
std::cout << c;
}
I want it to print BEGINENDasdf
(or ENDBEGINasdf
) but it prints asdf
.
Is there no other way than to do a manual for
using a qualified name?
Wrap
std::string
in your own type. By making it a template you can customise any existing container and add your own range logic to it. It's not even that different from your first attempt.Outputs