c++ - MSVC 2008 错误 'Type' 不是结构(尽管它是)

标签 c++ templates visual-c++ language-lawyer friend

以下 MWE 在 gcc 4.8.2 上编译但不在 MSVC 2008 上编译(公司政策)

struct B
{
};

struct A
{
    typedef B Handler;
};

template<typename T>
struct Foo
{
    typedef typename T::Handler  Type;
};

template<typename T>
struct Bar
{
    friend struct Foo<T>::Type;     // MSVC 2008 does not like this
    typedef typename Foo<T>::Type   Foo;
};

int main() 
{
}

MSVC 2008 错误

error C2649: 'Foo<T>::Type' : is not a 'struct'

这是一个编译器错误还是我在这里做了一些非法的事情?更重要的是有修复吗?

最佳答案

删除struct关键字并替换为typename:

template<typename T>
struct Bar
{
    friend typename Foo<T>::Type;     
    typedef typename Foo<T>::Type   Foo;
};

关于c++ - MSVC 2008 错误 'Type' 不是结构(尽管它是),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26870724/

相关文章:

c++ - C++ 算术 boost header 的使用

c++ - 在类定义之外为模板类定义 operator[]()(数组订阅)

c++ - 显示条形图(和其他时髦的图形)

c++ - 为什么我不能以此结束我的输入?

c++ - Opencv 使用 cv::Mat 创建新图像

c# - 如何使用 C# .net 从命令行调用带有多个参数的 C++ exe

c++ - 在 C++ 中使用无符号变量的真正优势是什么?

c++ - 获取函数参数类型作为元组

c++ - 基于 SFINAE 的特征来确定是否支持运算符 +

C++特殊变量名