无法从类型x转换为类型x?

Bal*_*ram 11 c++ function-pointers visual-studio

编译时(Microsoft Visual C++ 2005 Express)这段代码:

struct A
{
    template< typename T > static A Foo( void ) { return A(); }
    struct S
    {
        template< typename T > static S GetInstance( void )
        {
            S Result;
            Result.m_funcFoo = &A::Foo< T >;
            return Result;
        }
        A ( *m_funcFoo )( void );
    };
};

int main(int argc, char* argv[])
{
    A::S::GetInstance< int >();
}
Run Code Online (Sandbox Code Playgroud)

我收到一个C2440错误:

'=':无法从'A(__ cdecl*)(void)'转换为'A(__ cdecl*)(void)'

这对我来说没有意义.错误文本中指定的两种类型显然是相同的.此外,当将Foo返回值更改为时int,不会出现此类错误.

这是一个错误还是我做错了什么?

编辑: 那么,如果它是一个错误,有谁知道如何解决这个问题?也许通过使用演员阵容?我需要这段代码来编译......

Ben*_*igt 4

这是一个编译器错误。VC++正在做一些非常奇怪的事情。

例如,这会生成一条非常不同的错误消息:

struct A
{
    template< typename T > static struct A Foo( void ) { return A(); }
    struct S
    {
        template< typename T > static S GetInstance( void )
        {
            S Result;
            Result.m_funcFoo = &A::Foo< T >;
            return Result;
        }
        A ( *m_funcFoo )( void );
    };
};

sourceFile.cpp(5) : error C3856: 'A::Foo': class is not a class template
Run Code Online (Sandbox Code Playgroud)

这有效:

struct X {};

struct A
{
    template< typename T > static X Foo( void ) { return X(); }
    struct S
    {
        template< typename T > static S GetInstance( void )
        {
            S Result;
            Result.m_funcFoo = &A::Foo< T >;
            return Result;
        }
        X ( *m_funcFoo )( void );
    };
};
Run Code Online (Sandbox Code Playgroud)

显然它被名称混淆了A,它应该指的是基类。

添加 typedef 没有帮助,前向声明 也没有帮助struct A,将名称限定为::A或 也没有帮助struct A

奇怪的是,VC++7 编译得很好。

解决方法:像这样更改:

struct A
{
    template< typename T > static A Foo( void ) { return A(); }

    struct S;
};

struct A::S
{
    template< typename T > static S GetInstance( void )
    {
        S Result;
        Result.m_funcFoo = &A::Foo< T >;
        return Result;
    }
    A ( *m_funcFoo )( void );
};
Run Code Online (Sandbox Code Playgroud)

反转结果,现在 VC++8 编译正常,VC++7 生成相同的错误消息。

我认为不完整的类型和完成后的相同类型之间存在类型标识问题。

所有测试均使用Dinkumware 多编译器测试工具运行