Inconsistent typeid for a nested template function in Visual Studio

I came across a curious behavior in my program when compiling with the Visual Studio 2013 community, with CTP in November 2013. The following program compiles and prints "true", while the expected behavior was that it printed "false", which means GCC and clang do.

I tested this code on my installation, as well as on the following sites: http://webcompiler.cloudapp.net/ (claims the VS compiler is version 19, also prints "true"), http://codepad.org/ , http: //www.tutorialspoint.com/compile_cpp_online.php and several others.

I'm not sure what the correct behavior is, or the code is actually the correct C ++ code, so I'm completely dumb. If anyone could shed light on what is happening here, it would be fantastic.

#include <stdio.h>
#include <typeinfo>

template <typename T>
struct foo
{
    template <typename U>
    static void bar() {}
};

template <typename T>
void baz() {
    puts(typeid(&foo<T>::template bar<int>) == typeid(int) ? "true" : "false");
}

int main() {
    baz<double>();
}

Edit

Thanks to Reddit, I was able to bring this error to the STL, which reported that it had reported this, and it will be fixed in RTM VS 2015: <a4>

+4
source share
1 answer

VS2013 (tested with VS2013.4) really incorrectly gives the type of member function templates ( staticor not) like int. Consider the following simplified example, in which it should be pretty obvious that the code is correct C ++:

#include <typeinfo>
#include <iostream>

struct foo
{
    template<typename T>
    static void bar()
    { }
};

int main() {
    std::cout << typeid(&foo::bar<int>).name() << "\n";
    std::cout << typeid(int).name() << "\n";
    std::cout << (typeid(&foo::bar<int>) == typeid(int) ? "true\n" : "false\n");
}

Will be printed

int
int
true

, bar:

void (__cdecl*)(void)
int
false
+5

All Articles