MSVC 2008错误“类型”不是结构(尽管它是)

时间:2014-11-11 17:24:39

标签: 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'

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

2 个答案:

答案 0 :(得分:6)

删除struct关键字并将其替换为typename

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

答案 1 :(得分:4)

似乎无效。 [class.friend] / 3:

  

未声明函数的friend声明应具有一个声明   以下表格:
friend    elaborated-type-specifier ;
friend simple-type-specifier ;
{{1 } typename-specifier friend

但是,对详细类型说明符有一个重要的限制:[dcl.type.elab] / 2:

  

如果标识符解析为typedef-name [...]   精心设计的说明者是不正确的。

在这方面,GCC和Clang是错误的,令人惊讶的是,VC ++是对的 您可以使用第一个引用的第三个项目符号并使用typename-specifier。

;