This is an extension to this question from 2011: Range-based for loops and ADL
Using Visual Studio 2015, I'm not able to make a range-based for loop for a custom container using Argument Dependent Lookup (ADL).
I have made a very simple test case below with a custom container:
#include <vector>
namespace Foo
{
template <typename T>
class Container
{
public:
std::vector<T> values;
};
}
template <typename T>
typename std::vector<T>::iterator begin(Foo::Container<T>& foo)
{
return foo.values.begin();
}
template <typename T>
typename std::vector<T>::iterator end(Foo::Container<T>& foo)
{
return foo.values.end();
}
Using this container and ADL, the following test compiles perfectly fine:
int main(int argc, char* argv[])
{
Foo::Container<int> values;
for (auto it = begin(values); it != end(values); ++it)
{
...
}
return 0;
}
As it should. I'm not sure if ADL is even being utilized here, but regardless, it makes sense. From MSDN documentation, we have:
Keep in mind these facts about range-based for:
Automatically recognizes arrays.
Recognizes containers that have .begin() and .end().
Uses argument-dependent lookup begin() and end() for anything else.
From what I understand of ADL, and the documentation above, the following should also compile:
int main(int argc, char* argv[])
{
Foo::Container<int> values;
for (auto value : values)
{
...
}
return 0;
}
But it doesn't. Instead, I get the following errors:
error C3312: no callable 'begin' function found for type 'Foo::Container<int>'
error C3312: no callable 'end' function found for type 'Foo::Container<int>'
So what's going on here? Is my interpretation of ADL incorrect, or is this a bug with MSVC 14.0 Compiler?