简体   繁体   English

使用模板类的成员实例化MSVC ++中的模板默认参数

[英]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. 下面的代码是我试图从GCC / G ++移植到Microsoft Visual C ++ 2010的大型项目的简化示例。它可以在G ++上很好地编译,但是在MSVC ++上,它会引发错误,并且我很难理解为什么。

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: 来自MSVC ++的错误消息是:

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. 我尝试了一些更改来缩小问题的范围,虽然我不太了解,但是发现的是:如果在第1行中,我将A::C::D替换为A::C ,效果很好。 If I replace template <typename> class E with just typename E and make that foo<E> , it works fine. 如果我更换template <typename> class E只用typename E并作出foo<E>它工作正常。 If explicitly specify the second template argument to foo in line 8, like so, it works fine: 如果像这样在第8行中显式指定foo的第二个模板参数,则效果很好:

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; 而且,如果我在第1行中将A::C::D替换为无害的名称,例如typename B = A ,但添加另一种使用A::C::D作为typedef typename A::C::D qux; to the definition of foo , that also works fine. 按照foo的定义,也可以正常工作。

Any ideas? 有任何想法吗? What bit of C++ rules am I missing? 我缺少什么C ++规则?

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: 根据jpalecek的建议,我将其报告为Microsoft的错误,并且Microsoft已确认这确实是其编译器中的错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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