Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does MSVC fail to compile this template function?

Tags:

I ran into a problem porting some code to MSVC, which puzzles me. As far as I know, the code should be legal, and Clang compiles it just fine.

I've narrowed it down to the following:

enum E {
    x
};

template <typename T>
struct traits {
    static const E val = x;
};

template <E e>
struct S {
    S(){};
};

template <typename T>
S<traits<T>::val> foo(T t);

int main() {
    char c = 0;
    foo(c);
}

Note that after compilation, the code is expected to yield a linker error (I stripped away the definition of the function foo to keep the sample minimal), but it should compile cleanly as far as I know.

However, MSVC gives me this error:

error C2893: Failed to specialize function template 'S::val> foo(T)'

So my question:

  • is MSVC by any chance correct in rejecting the code? (and if so, why?)
  • if not, can anyone narrow down what it is doing wrong? As in, is it a language feature they don't implement at all (such as two-phase name lookup for templates), or "just" a plain bug in their implementation of a feature they claim to support?

I've reproduced the problem on VC++ 2010 and 2012.