Reputation: 2734
I have stumbled upon a curious behavior in my program when compiling with Visual Studio 2013 Community, with the November 2013 CTP. The following program compiles and prints "true", while the expected behavior was that it'd print "false", which is what GCC and clang do.
I have tested this code on my setup, as well as on the following sites: http://webcompiler.cloudapp.net/ (claims VS compiler version 19, also prints "true"), http://codepad.org/ , http://www.tutorialspoint.com/compile_cpp_online.php , and a few others.
I'm not sure what the correct behavior here is, or whether the code below is actually correct C++ code, so I'm quite stumped. If anyone could shed some light on what is going here, that 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 managed to bring this bug to the attention of STL, who says he has reported it and it will be fixed in the VS 2015 RTM: http://www.reddit.com/r/cpp/comments/2zs2ob/vc_2015_rtm_what_do_you_want_it_to_have/cpm01wr
Upvotes: 4
Views: 643
Reputation: 11126
VS2013 (tested with VS2013.4) does indeed incorrectly give the type of member function templates (static
or not) as 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");
}
Which will print
int
int
true
instead of what happens when removing the template parameter of bar
:
void (__cdecl*)(void)
int
false
Upvotes: 4