Leaving aside policy and semantic issues about whether to specialize a function template from the std ,
The following snippet does not work:
class stackiterator {}; struct stack { stackiterator Begin() { return stackiterator{};} }; #include <iterator> namespace std { template <> stackiterator begin<stack>(stack& S) { return S.Begin(); } }
However, the following snippet works fine:
class stackiterator {}; struct stack { stackiterator Begin() { return stackiterator{};} }; #include <iterator> namespace std { template <> stackiterator begin<stack>(stack& S) { return S.Begin(); } }
The key difference is the presence of Begin() vs Begin() as a member function from stack . std::begin() is defined as:
template <class C> auto begin(C& c) -> decltype(c.begin()); template <class C> auto begin(const C& c) -> decltype(c.begin());
When you specialize in a function template, you should still keep the return type the same. Unless you have Begin() as a member of the stack , the compiler does not know how to determine the return type.
This is the reason for the error generated by the compiler.
By the way, there is another SO post that partially responds to what can be specialized and what cannot be specialized.
Looking at the part of the standard that deals with std::begin() , section 24.3, I see nothing about not specializing in std::begin() .
R sahu
source share