【问题标题】:difference between typeids of template and non-template member function pointers模板和非模板成员函数指针的typeids的区别
【发布时间】: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


【解决方案1】:

这显然是 VS 2012 中的编译器错误。它看起来与链接到 this SO question 的 #775434 "decltype is buggy in VS 2012" 密切相关。如果这确实是错误,那么除了升级到 VS 2015 之外,您还可以尝试类似的解决方法 (src):

template <typename T> T msvc_decltype_helper(T); // Never implemented.
#define FN_DECLTYPE(...) decltype(::msvc_decltype_helper(__VA_ARGS__))

即试试typeid(msvc_decltype_helper(&amp;foo::func1&lt;3&gt;)).name() 的输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多