I have run into some strange behavior. I am not sure if this should work or not. Note that I am not trying to make it work, I am rather concerned with theoretical side. I am using Visual C++ 2010. Observe the following code
template <class T_>
ostream &operator <<(std::ostream &out,
typename SequenceCheckResult<T_>::directiontype const &direction) {
switch(direction) {
case SequenceCheckResult<T_>::None:
out<<"none o";
break;
case SequenceCheckResult<T_>::Horizontal:
out<<"horizontal _";
break;
case SequenceCheckResult<T_>::Vertical:
out<<"vertical |";
break;
case SequenceCheckResult<T_>::ForwardDiagonal:
out<<"forward diagonal \\";
break;
case SequenceCheckResult<T_>::BackwardDiagonal:
out<<"backward diagonal /";
break;
}
return out;
}
Obviously the template class SequenceCheckResult contains an enum named directiontype. Now the code above does not get instantiated and integer value of the variable is printed. Even if I provide specialization, it still does not work. If I create the following function, it is used and the textual value is printed. Notice that the following code and int specialization of the above function only differs by template<> line and <int> following operator <<.
ostream &operator << (std::ostream &out,
SequenceCheckResult<int>::directiontype const &direction) {
switch(direction) {
case SequenceCheckResult<int>::None:
out<<"none o";
break;
case SequenceCheckResult<int>::Horizontal:
out<<"horizontal _";
break;
case SequenceCheckResult<int>::Vertical:
out<<"vertical |";
break;
case SequenceCheckResult<int>::ForwardDiagonal:
out<<"forward diagonal \\";
break;
case SequenceCheckResult<int>::BackwardDiagonal:
out<<"backward diagonal /";
break;
}
return out;
}
I wish to know why this happens, possible causes are:
- Compiler bug
- Implementation defines this behavior
- Standards define this behavior
- I am missing something
Thanks in advance
Your approach does not and cannot work. You cannot deduce a “container based on an element”!
For a simple argument, consider this thought experiment:
Now if I call
deduce_me(5), how are we supposed to deduce eitherFooorBar?