I wrote C ++ code to create vector math. It only matters a thin wrapper around the std::array instance. I wanted to overload the non-member begin() function to return the iterator to the beginning of the support array. To do this, I wrote a simple friend function with the return type of auto and the return type of return with decltype , which just redirected the call along with the member variable.
This did not compile, and I could not understand why. I started to deal with a smaller example and found that the following code compiles under g ++ 4.7, but not under the latest Visual Studio 2012 Professional.
#include <iostream> #include <array> template <typename T, size_t size> class MyClass { private: std::array<T, size> elts; public: friend auto begin(MyClass &a) -> decltype (std::begin(a.elts)) { return std::begin(a.elts); } }; int main(void) { MyClass<int, 8> instance; auto it = begin(instance); std::cout << *it << std::endl; return 0; }
This code was odd only in g ++ if the private elts appeared before the begin() function declaration.
Anyway, which compiler is here? Visual Studio or g ++?
Change The compilation error provided by VS2012 was error C2228: left of '.elts' must have class/struct/union
source share