According to range based for loop documentation here:
begin_exprandend_exprare defined as follows:
- If range_expression is an expression of array type, then
begin_expris__rangeandend_expris(__range + __bound), where__boundis the number of elements in the array (if the array has unknown size or is of an incomplete type, the program is ill-formed)- If
range_expressionis an expression of a class typeCthat has a member namedbeginand/or a member namedend(regardless of the type or accessibility of such member), thenbegin_expris__range.begin) andend_expris__range.end();- Otherwise,
begin_exprisbegin(__range)andend_exprisend(__range), which are found via argument-dependent lookup (non-ADL lookup is not performed).
However, if I define begin() and end() for a pointer type, it fails to work.
Example
#include <iostream>
using LPCSTR = char const*;
LPCSTR begin(LPCSTR str)
{
return str;
}
LPCSTR end(LPCSTR str)
{
return str + strlen(str);
}
int main()
{
LPCSTR text = "Hello, world!\n";
for (auto c : text)
{
std::cout << c;
}
}
Error(s):
source_file.cpp:18:17: error: invalid range expression of type 'const char *'; no viable 'begin' function available
for (auto c : text)
^ ~~~~
1 error generated.
I don't see any reference that pointers are excluded from the ADL, so what reason would there be as to why this isn't working?