MSVC++中可能存在编译器错误



我相信我在MSVC++中发现了一个编译器错误(VS 2013之前存在)。在我报告它之前,我想检查一下它是否确实是一个错误。

以下代码:

#include <map>
using std::map;
template <typename T>
class A
{
public:
    typedef T StoredType;
};
template <typename T>
map<typename T::StoredType, int> foo()
{
    map<typename T::StoredType, int> ret;
    return ret;
}  
template<>
map<char, int> foo<A<char>>()
{
    map<char, int> ret;
    return ret;
} // Error on this line
int main(int, char**)
{
    return 0;
}

产生编译错误:

1>d:documentsvisual studio 2010projectsprojprojsource1.cpp(24): error C2785: 'std::map<T::StoredType,int> foo(void)' and 'std::map<_Kty,_Ty> foo(void)' have different return types
1>          with
1>          [
1>              _Kty=char,
1>              _Ty=int
1>          ]
1>          d:documentsvisual studio 2010projectsprojprojsource1.cpp(13) : see declaration of 'foo'
1>          d:documentsvisual studio 2010projectsprojprojsource1.cpp(20) : see declaration of 'foo'
1>d:documentsvisual studio 2010projectsprojprojsource1.cpp(24): error C2912: explicit specialization; 'std::map<_Kty,_Ty> foo<A<T>>(void)' is not a specialization of a function template
1>          with
1>          [
1>              _Kty=char,
1>              _Ty=int,
1>              T=char
1>          ]

然而,它在我看来还可以,在ideone.com上编译得很好。这是一个bug吗?它应该干净地编译吗?

有趣的是,类专业化上的相同东西工作得很好:

#include <map>
using std::map;
template <typename T>
class A
{
public:
    typedef T StoredType;
};
template <typename T>
struct Z
{
    map<typename T::StoredType, int> foo()
    {
        map<T::StoredType, int> ret;
        return ret;
    }  // Error on this line
};
template<>
struct Z<A<char>>
{
    map<char, int> foo()
    {
        map<char, int> ret;
        return ret;
    }
};
int main(int, char**)
{
    return 0;
}

如果映射是在模板中定义的,那么它看起来也很好:

#include <map>
using std::map;
template <typename T>
class A
{
public:
    typedef map<T, int> MapType;
};
template <typename T>
typename T::MapType foo()
{
    T::MapType ret;
    return ret;
}
template<>
map<char, int> foo<A<char>>()
{
    map<char, int> ret;
    return ret;
}
int main(int, char**)
{
    return 0;
}

因此,关于bug的假设似乎是可能的。

相关内容

最新更新