Attempting to modify the code from this page.
Here's the problem code:
#include <iostream>
#include <array>
template<class T>
class const_reverse_wrapper
{
public:
const_reverse_wrapper (const T& cont)
: container_(cont)
{
}
decltype( container_.rbegin() ) begin() const
{
return container_.rbegin();
}
decltype( container_.rend() ) end()
{
return container_.rend();
}
private:
const T & container_;
};
template<class T>
class reverse_wrapper
{
public:
reverse_wrapper (T & cont)
: container_(cont)
{
}
decltype( container_.rbegin() ) begin()
{
return container_.rbegin();
}
decltype( container_.rend() ) end()
{
return container_.rend();
}
private:
T & container_;
};
template<class T>
const_reverse_wrapper<T> reversed (const T & cont)
{
return const_reverse_wrapper<T>(cont);
}
template<class T>
reverse_wrapper<T> reverse (T & cont)
{
return reverse_wrapper<T>(cont);
}
int main (int argc, char * argv[])
{
std::array<int,4> a = { 1, 2, 3, 4 };
for (int i : a)
std::cout << i;
return 0;
}
When I compile it, I get these errors:
> g++ -std=c++0x test2.cpp
test2.cpp:13:15: error: 'container_' was not declared in this scope
test2.cpp:13:15: error: 'container_' was not declared in this scope
test2.cpp:18:15: error: 'container_' was not declared in this scope
test2.cpp:18:15: error: 'container_' was not declared in this scope
test2.cpp:36:15: error: 'container_' was not declared in this scope
test2.cpp:36:15: error: 'container_' was not declared in this scope
test2.cpp:41:15: error: 'container_' was not declared in this scope
test2.cpp:41:15: error: 'container_' was not declared in this scope
When I move the private sections before the public sections in each class, the errors go away.
template<class T>
class const_reverse_wrapper
{
private: // <-----
const T & container_; // <-----
public:
const_reverse_wrapper (const T& cont)
: container_(cont)
{
}
decltype( container_.rbegin() ) begin() const
{
return container_.rbegin();
}
decltype( container_.rend() ) end()
{
return container_.rend();
}
};
template<class T>
class reverse_wrapper
{
private: // <-----
T & container_; // <-----
public:
reverse_wrapper (T & cont)
: container_(cont)
{
}
decltype( container_.rbegin() ) begin()
{
return container_.rbegin();
}
decltype( container_.rend() ) end()
{
return container_.rend();
}
};
I've tried compiling with MinGW GCC 4.6.2 and 4.7.0 and get the same results. Is this a bug, or is there something else going on?