如何直接获得class template convert operator的函数指针?

sevecol 2003-08-29 05:17:12
有个类
class A
{
public:
template<typename T>
operator T*()
{
return new T(100);
}

};

//
比如我想直接获得A::operator int*的函数指针,该怎么做?
3q
...全文
26 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
sevecol 2003-09-04
  • 打赏
  • 举报
回复
我用vc7.1也不行,看样子以后得多装些编译器,不能一棵树上吊死。
xueweizhong 2003-09-04
  • 打赏
  • 举报
回复
int* (A::*p)() = &A::operator int*;
int* (A::*p)() = &A::operator int*<int>;
int* (A::*p)() = &A::template operator int*<int>;

这三句在g++上编译通过。

但是VC6.0好象还是对这个东西很愤怒:“这是什么?!~`/???”
---> fata error .....

sevecol 2003-09-04
  • 打赏
  • 举报
回复
其他的都能通过?
那么和我想的一样,是编译器的问题了。
xueweizhong 2003-09-04
  • 打赏
  • 举报
回复
我试了一下,
BCC,
CC,
EDG comeaucomputing online compiler
都编译通过。

g++和VC好象对付不了这个稍复杂些的东西:

struct A
{
template <class T>
operator T* ()
{
return 0;
}
// return value T*
// argument : NULL
// class A
// equivalent to
// template <typename T>
// T* f()
// type is T* (A::)()

};

int main()
{
&A::operator int*;
int* (A::*p)() = &A::operator int*;
}
sevecol 2003-09-04
  • 打赏
  • 举报
回复
最后一次up
reinhard_liu 2003-08-31
  • 打赏
  • 举报
回复
gz
sevecol 2003-08-30
  • 打赏
  • 举报
回复
up..........
sevecol 2003-08-29
  • 打赏
  • 举报
回复
一般的template function我能显式取到地址

可是template convert operator我没有办法。

typedef int* (A::*q)();
q bb=reinterpret_cast<q>(&A::operator int*);//编译器报错

而我不知道如何给这类函数显式的指定模板类型。
bigdoors 2003-08-29
  • 打赏
  • 举报
回复
Think in C++中有个取template function地址的例子,可以看看
// Taking the address of a function generated
// from a template.

template <typename T> void f(T*) {}

void h(void (*pf)(int*)) {}

template <class T>
void g(void (*pf)(T*)) {}

int main() {
// Full type exposition:
h(&f<int>);
// Type induction:
h(&f);
// Full type exposition:
g<int>(&f<int>);
// Type inductions:
g(&f<int>);
g<int>(&f);
} ///:~
reinhard_liu 2003-08-29
  • 打赏
  • 举报
回复
我当时问这个问题只是看书的时候突发奇想而已,我想了解一下 对于c++的理解也许会深入一点,具体我也没考虑过这个指针有什么用,不过还是很想知道答案。
sevecol 2003-08-29
  • 打赏
  • 举报
回复
我也只是因为有人问的一个帖子,才想知道有没有办法直接获得。

确实函数指针确实用处不是很大了。
rtdb 2003-08-29
  • 打赏
  • 举报
回复
看楼主对函数指针如此兴趣, 借宝地讨论一下:
在C++中使用函数指针还有多少意义?

在C中,函数指针可以组成数组, 或者类似回调函数,传个算法进去什么的。

在C++中, 有对象, 有多态, 我觉得应该不用函数指针了。

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧