The code listed below is valid to the best of my knowledge, and compiles under both Clang and GCC.
However, Visual C++ produces a cryptic error message instead:
error C2893: Failed to specialize function template 'S::val> foo(T)'
The problem only occurs if the struct S is templated on an enum type. Change it to 'template <int e>' instead, and it compiles just fine.
I posted about the eror here as well: