简体   繁体   中英

Using member of template class to instantiate template default parameter in MSVC++

The following piece of code is a reduced sample from the large project I'm trying to port from GCC/G++ to Microsoft Visual C++ 2010. It compiles fine with G++, but with MSVC++, it throws errors, and I'm having trouble understanding why.

template <typename A, typename B = typename A::C::D>    // line 1
struct foo
{
  typedef int type;
};

template <template <typename> class E, typename T>
typename foo<E<T> >::type                               // line 8
bar(){}

The error messages from MSVC++ are:

example1.cpp(1) : error C2027: use of undefined type 'E<T>'
example1.cpp(8) : error C2146: syntax error : missing ',' before identifier 'D'
example1.cpp(8) : error C2065: 'D' : undeclared identifier

I've tried a few changes to narrow down the problem a bit, and while I don't fully understand it, here's what I've discovered: If in line 1 I replace A::C::D with A::C , it works fine. If I replace template <typename> class E with just typename E and make that foo<E> , it works fine. If explicitly specify the second template argument to foo in line 8, like so, it works fine:

typename foo<E<T>, typename E<T>::C::D>::type          // line 8

And, if I replace the use of A::C::D with something innocuous like typename B = A in line 1, but add a different use of A::C::D as typedef typename A::C::D qux; to the definition of foo , that also works fine.

Any ideas? What bit of C++ rules am I missing?

I reported this as a bug with Microsoft as per jpalecek's suggestion, and Microsoft has confirmed that it is indeed a fault in their compiler:

https://connect.microsoft.com/VisualStudio/feedback/details/576196

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM