Consider:
#include <iostream>
#include <vector>
class A
{
public:
typedef bool TAll;
static TAll All;
typedef std::vector<int> TVec;
static TVec m_sVec;
static TVec::iterator begin() { return m_sVec.begin(); }
static TVec::iterator end() { return m_sVec.end(); }
};
A::TVec A::m_sVec;
A::TAll A::All;
A::TVec::iterator begin(A::TAll& all) { return A::begin(); }
A::TVec::iterator end(A::TAll& all) { return A::end(); }
int _tmain(int argc, _TCHAR* argv[])
{
A::m_sVec.push_back(1);
A::m_sVec.push_back(2);
A::m_sVec.push_back(3);
for (auto a : A::All) {
//for (auto a = begin(A::All); a != end(A::All); a++) {
std::cout << a << std::endl;
}
return 0;
}
The version with the range based for loop (so this code as-is) gives me the following error in MSVC2013:
1><snip>: error C3312: no callable 'begin' function found for type 'A::TAll'
1><snip>: error C3312: no callable 'end' function found for type 'A::TAll'
GCC (4.8.3) says (last two lines):
/usr/include/c++/4.8.3/initializer_list:99:5: note: template<class _Tp> constexpr cons
t _Tp* std::end(std::initializer_list<_Tp>)
end(initializer_list<_Tp> __ils) noexcept
^
/usr/include/c++/4.8.3/initializer_list:99:5: note: template argument deduction/subs
titution failed:
main.cpp:31:18: note: mismatched types 'std::initializer_list<_Tp>' and 'bool'
for (int a : A::All) {
The 'normal' for loop that uses the functions (the one that is commented out) works (well, after dereferencing 'a' inside the loop of course); it is my understanding of the standard and the Stroustroup that they should be equivalent. But I guess not. So what is the problem here? Thanks.