【发布时间】:2015-11-19 15:10:52
【问题描述】:
我试图找出 C++ (VS2012) 中成员函数指针类型转换问题的根本原因。
我已将其缩小到指向模板成员函数和非模板成员函数的指针typeid 的区别。考虑代码:
#include <typeinfo>
#include <iostream>
class foo
{
public:
template<int k>
void func1() {}
void func2() {}
};
typedef void (foo::*foofunc)();
int main()
{
//this gives "int"
std::cout<<typeid(&foo::func1<3>).name()<<std::endl;
//this gives "void (__thiscall *)(void)"
std::cout<<typeid(&foo::func2).name()<<std::endl;
return 0;
}
func2 具有预期值。为什么指向func1 的指针具有int 类型,而两者都是同一个类的成员函数?
注意:这只发生在 Windows 上。
【问题讨论】:
-
在 GCC 4.8 上,它们都打印
M3fooFvvE -
@PaoloM:是的。我不确定为什么这个名字晦涩难懂,但至少它们似乎与 GCC 具有相同的类型。
-
晦涩是什么意思?顺便说一句,这是来自cppreference:不能保证相同类型的 typeid 表达式的所有评估都会引用相同的 std::type_info 实例,尽管那些 type_info 的 std::type_info::hash_code对象将是相同的,它们的 std::type_index 也是如此。
标签: c++ function templates pointers visual-studio-2012