Gha*_*nPL 4 c++ templates visual-studio visual-c++
在使用Visual Studio 2013社区编译2013年11月的CTP时,我偶然发现了我的程序中的奇怪行为.下面的程序编译并打印"true",而预期的行为是它打印"false",这就是GCC和clang所做的.
我已在我的设置以及以下网站上测试了此代码:http://webcompiler.cloudapp.net/(声称VS编译器版本19,也打印"true"),http://codepad.org/,http://www.tutorialspoint.com/compile_cpp_online.php和其他一些人.
我不确定这里的正确行为是什么,或者下面的代码是否真的是正确的C++代码,所以我很难过.如果有人能够对这里发生的事情有所了解,那就太棒了.
#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>();
}
Run Code Online (Sandbox Code Playgroud)
编辑:
感谢Reddit,我设法将这个bug引起了STL的注意,STL已经报告了它并将在VS 2015 RTM中修复:http://www.reddit.com/r/cpp/comments/2zs2ob/ vc_2015_rtm_what_do_you_want_it_to_have/cpm01wr
VS2013(使用VS2013.4测试)确实错误地给出了成员函数模板的类型(static或不是)int.考虑以下简化示例,其中代码是正确的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");
}
Run Code Online (Sandbox Code Playgroud)
哪个会打印
int
int
true
Run Code Online (Sandbox Code Playgroud)
而不是删除模板参数时发生的情况bar:
void (__cdecl*)(void)
int
false
Run Code Online (Sandbox Code Playgroud)