I believe I've found a compiler bug in MSVC++ (present up to VS 2013). I want to check that it is indeed a bug before I report it.
The following code:
#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;
}
Produces a compile error:
1>d:\documents\visual studio 2010\projects\proj\proj\source1.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:\documents\visual studio 2010\projects\proj\proj\source1.cpp(13) : see declaration of 'foo'
1> d:\documents\visual studio 2010\projects\proj\proj\source1.cpp(20) : see declaration of 'foo'
1>d:\documents\visual studio 2010\projects\proj\proj\source1.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> ]
However, it looks OK to me, and compiles fine on ideone.com. Is it a bug? Should it compile cleanly?
Microsoft C++ Compiler (MSVC) This is the default compiler for most Visual Studio C++ projects and is recommended if you are targeting Windows.
Is MinGW (MinGW-64) better than Cygwin in terms of MSVC alternative for creating Windows application? If your program will run only on Windows, then MinGW is likely the better choice. MinGW is designed to create Windows applications. It doesn't require users to install additional software to run your application.
By default, the MSVC compiler treats all files that end in . c as C source code, and all files that end in . cpp as C++ source code. To force the compiler to treat all files as C no matter the file name extension, use the /TC compiler option.
Compiler supportVisual Studio 2019 supports all C++20 features through its /std:c++latest option, as of version 16.10. 0. An option /std:c++20 to enable C++20 mode is added in version 16.11. 0.
What is interesting that the same thing on a class specialization works fine:
#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;
}
If the map is defined in the template then it also seems fine:
#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;
}
So the assumption about the bug seems to be possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With