Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible compiler bug in MSVC++

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?

like image 998
Chowlett Avatar asked Oct 30 '14 09:10

Chowlett


People also ask

What compiler does MSVC use?

Microsoft C++ Compiler (MSVC) This is the default compiler for most Visual Studio C++ projects and is recommended if you are targeting Windows.

Should I use MinGW or MSVC?

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.

Can MSVC compile C?

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.

Does MSVC support C++ 20?

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.


1 Answers

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.

like image 193
Rudolfs Bundulis Avatar answered Oct 11 '22 22:10

Rudolfs Bundulis