想在编译期知道struct是否定义了某个函数,请问用C++(模板)能解决这个问题吗?

wfh_178 2009-02-26 11:04:45
在编译期可以知道struct是否含有某个成员变量而采用所谓的编译期多态,那么是否可以知道struct含有某个函数呢?
...全文
173 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
lin_style 2009-02-26
  • 打赏
  • 举报
回复
日啊。2楼的地址开了半天没打开。
nineforever 2009-02-26
  • 打赏
  • 举报
回复
成员函数指针的书写格式
void (T::*)(int)
返回值,类名,参数
wfh_178 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 xuhb95083023 的回复:]
上面的额

template <>
smart_method {
typedef other_method real_method;
}

写错了,应该是

template <>
smart_method <false> {
typedef other_method real_method;
}
[/Quote]

这样也不行啊,编译不能通过
wfh_178 2009-02-26
  • 打赏
  • 举报
回复
晕,我不知道怎么改,难道是T.f???
xuhb95083023 2009-02-26
  • 打赏
  • 举报
回复
上面的额

template <>
smart_method {
typedef other_method real_method;
}

写错了,应该是

template <>
smart_method<false> {
typedef other_method real_method;
}
疯哥哥 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xuhb95083023 的回复:]
引用 2 楼 nineforever 的回复:
http://blogs.msdn.com/xiangfan/archive/2009/02/09/c-template-trick-detecting-the-existence-of-class-member-at-compile-time.aspx


赞一个,结合宏这个做法太帅了
[/Quote]
学习~~
疯哥哥 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xuhb95083023 的回复:]
引用 2 楼 nineforever 的回复:
http://blogs.msdn.com/xiangfan/archive/2009/02/09/c-template-trick-detecting-the-existence-of-class-member-at-compile-time.aspx


赞一个,结合宏这个做法太帅了
[/Quote]
学习~~
xuhb95083023 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wfh_178 的回复:]
现实的问题是,我有很多这样的class,其中一些class是类似list类型(这里叫他们A类),另外一些是些各种各样的非list类型的class(B类).

A类的一个共同点是都有一个length()公有函数,B类则是各式各样的元素类.现在我想写一个通用的比较函数compare().

对一个类unknown:

1.如果有length()函数,就循环逐个比较,如果list中有list,还可能递归;
2.如果没有,就是元素类,就采用其他的方法.

因为这样的类太多,而且将…
[/Quote]


struct loop_compare_method{
static void execute();
}

struct other_method {
static void execute;
}

template<bool>
smart_method {
typedef loop_compare_method real_method;
}

template <>
smart_method<false> {
typedef other_method real_method;
}

// 调用方法:
smart_method<这里填写根据上面的帖子判断有没有lengh函数的代码>::real_method::execute()


nineforever 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 wfh_178 的回复:]
引用 14 楼 xuhb95083023 的回复:
引用 12 楼 wfh_178 的回复:
上面帖那个方法只针对静态共有函数有效,如果是公有非静态函数就不行了


谁说不行,也可以的


我在vc2005下面编译,如果去掉static就判断为0了
[/Quote]

模板参数里的函数指针类型要跟着改的
wfh_178 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 xuhb95083023 的回复:]
引用 12 楼 wfh_178 的回复:
上面帖那个方法只针对静态共有函数有效,如果是公有非静态函数就不行了


谁说不行,也可以的
[/Quote]

我在vc2005下面编译,如果去掉static就判断为0了
wfh_178 2009-02-26
  • 打赏
  • 举报
回复
现实的问题是,我有很多这样的class,其中一些class是类似list类型(这里叫他们A类),另外一些是些各种各样的非list类型的class(B类).

A类的一个共同点是都有一个length()公有函数,B类则是各式各样的元素类.现在我想写一个通用的比较函数compare().

对一个类unknown:

1.如果有length()函数,就循环逐个比较,如果list中有list,还可能递归;
2.如果没有,就是元素类,就采用其他的方法.

因为这样的类太多,而且将来随时可能还要增加,所以每个都特化不方便也没有兼容性,所以寻求一种方法,感觉模板能做到,但是不知如何实现

xuhb95083023 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wfh_178 的回复:]
上面帖那个方法只针对静态共有函数有效,如果是公有非静态函数就不行了
[/Quote]

谁说不行,也可以的
nineforever 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 wfh_178 的回复:]
就是我想针对不同的类实现不同的操作,但是这些类只有其中的函数定义不同,c++的模板能实现这一功能吗?请熟悉模板的大虾们指点
[/Quote]

你的需求直接特化就可以了
wfh_178 2009-02-26
  • 打赏
  • 举报
回复
上面帖那个方法只针对静态共有函数有效,如果是公有非静态函数就不行了
wfh_178 2009-02-26
  • 打赏
  • 举报
回复
就是我想针对不同的类实现不同的操作,但是这些类只有其中的函数定义不同,c++的模板能实现这一功能吗?请熟悉模板的大虾们指点
wfh_178 2009-02-26
  • 打赏
  • 举报
回复
我的原始问题是这样的:

/// A and B's definition are fixed.
struct A{
int x(){return 1;};
};

struct B{
int y(){return 0;};
};


// function i should implement
////////////////////////////////////////
template<typename T>
struct Func(const T& p){
/// do something with A
}

template<typename T>
struct Func(const T& p){
/// do something with B
}

////////////////////////
A a;
B b;
Func(a); /// 我想要这个调用第一个Func
Func(b); /// 我想要这个调用第二个Func



jakqigle 2009-02-26
  • 打赏
  • 举报
回复

struct里可以带函数的。
struct myStruct{
//默认为public的
myStruct(int d):data(d){}
myStruct(const myStruct &mySt){...}
int getData(){ return data;}
private:
int data;
};
xuhb95083023 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xuhb95083023 的回复:]
引用 2 楼 nineforever 的回复:
http://blogs.msdn.com/xiangfan/archive/2009/02/09/c-template-trick-detecting-the-existence-of-class-member-at-compile-time.aspx


赞一个,结合宏这个做法太帅了
[/Quote]

发现##这个东西太有用了
jakqigle 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hairetz 的回复:]
struct里可以带函数的?
[/Quote]
struct里可以带函数的。
struct myStruct{
//默认为public的
myStruct(int d):data(d){}
myStruct(const myStruct &mySt){...}
int getData(){ return data;}
private:
int data;
};
waizqfor 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 nineforever 的回复:]
http://blogs.msdn.com/xiangfan/archive/2009/02/09/c-template-trick-detecting-the-existence-of-class-member-at-compile-time.aspx
[/Quote]
UP!~~ 讲的不错
加载更多回复(10)

64,654

社区成员

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

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