Clang 3.2 error or incorrect code ?

The snippet below is successfully compiled by gcc 4.7.2:

template <typename T, unsigned N>
struct Helper {};

template <typename T, unsigned N >
Helper< T, N > GetHelper() { return Helper< T, N > (); }

template < typename T, unsigned N >
struct X {
    X( Helper< T, N > h = GetHelper<T, N>() ) {}
};

With clang 3.2 though it spews a lot of errors:

Compilation finished with errors:
source.cpp:9:40: error: unknown type name ‘N’
X( Helper< T, N > h = GetHelper<T, N>() ) {}
^
source.cpp:9:41: error: expected ‘)’
X( Helper< T, N > h = GetHelper<T, N>() ) {}
^
source.cpp:9:6: note: to match this ‘(’
X( Helper< T, N > h = GetHelper<T, N>() ) {}
^
source.cpp:9:38: error: expected ‘>’
X( Helper< T, N > h = GetHelper<T, N>() ) {}
^
3 errors generated.

for the exact position of the carets, you can see: http://liveworkspace.org/code/3EALA5$27

  • the first caret is positionned under the N of GetHelper<T, N>()
  • the second caret is positionned under the > of GetHelper<T, N>()
  • the fourth caret is positionned under the comma of GetHelper<T, N>()

I suspect a clang bug, given that gcc manages to compile the code.

Note: related SO question => http://stackoverflow.com/q/14401308/147192

– Matthieu

See PR13657

Thanks Richard!

I had not considered the ambiguity and I understand much better why the compiler diagnosis were so off the bat: now it makes sense that it would try to use N as a type and complain.

The suggested work-around (parentheses) works like a charm.

– Matthieu