模板成员函数的地址 [英] Address of templated member function

查看:56
本文介绍了模板成员函数的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,如何找到成员函数f的地址

In the example below, how do I find the address of the member function f

template<typename HANDLER>
void serialize(HANDLER &h) {
    //  Compiler error (gcc 4.8.1)
    //  test.cxx: In function ‘void serialize(HANDLER&)’:
    //  test.cxx:9:26: error: expected primary-expression before ‘int’
    //       auto x = &HANDLER::f<int>;
    //                            ^
    //  test.cxx:9:26: error: expected ‘,’ or ‘;’ before ‘int’
    auto x = &HANDLER::f<int>;
}

struct HandlerA {
    template<typename T> void f() { }
};

struct HandlerB {
    template<typename T> void f() { }
};

struct HandlerC {
    template<typename T> void f() { }
};

int main() {
    HandlerA a;
    HandlerB b;
    HandlerC c;

    a.f<int>();
    b.f<int>();
    c.f<int>();

    serialize(a);
    serialize(b);
    serialize(c);
}


推荐答案

您需要告诉编译器 f 是模板,以便它可以正确解析该函数​​:

You need to tell the compiler that f is a template so that it can parse the function correctly:

auto x = &HANDLER::template f<int>;

这篇关于模板成员函数的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆