非类型模板参数问题

iamnobody 2011-11-10 10:25:25


template <class Type,typename MemberType,MemberType Type::*Member>
struct member_data :public std::unary_function<Type,ElemType&>
{
inline MemberType& operator()(Type& x){
return x.*Member;
}
};

用法:
struct A{ int a,b;};
member_data<A,int,&A::b> getb;
A object;
getb(object) = 0;//返回b的引用。
有没有办法让memdata所需要的模板参数少一些,像这样:
member_data<&A::b> getb;
如果是函数的话是可以的:
template <class Type,typename MemberType>
MemberType& fun(MemberType Type::*Member){
Type x;
return x.*Member;
};

模板类的话应该怎么做呢?
另外如果是函数参数的话呢?(我知道 std::mem_fun();但是它不能被内联,不适合用来做适配器):
template <class Type,typename Result,Result (Type::*pFun)() >
struct member_fun :public std::unary_function<Type,Result>
{
inline Result operator()(Type& x){
return (x.*pFun)();
};
};

...全文
580 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
At小明同学 2013-10-28
  • 打赏
  • 举报
回复
iamnobody 2011-11-13
  • 打赏
  • 举报
回复
[Quote=引用 32 楼 dizuo 的回复:]
关注一下。
[/Quote]

我也关注一下。
iamnobody 2011-11-11
  • 打赏
  • 举报
回复
。。。。加到最大分数,求解。。。。

  • 打赏
  • 举报
回复
[Quote=引用 29 楼 fallening 的回复:]

引用 28 楼 akirya 的回复:

引用 24 楼 mingliang1212 的回复:

typedef [](test&amp; x){ x.func() ; } myfun;
myfun(x);

这个貌似不能typedef的。

再套一层 decltype 差不多就可以 typedef 了
[/Quote]
再套的话 恐怕不能inline了
fallening 2011-11-11
  • 打赏
  • 举报
回复
[Quote=引用 27 楼 mingliang1212 的回复:]

引用 25 楼 taodm 的回复:
那不就是抄抄boost::bind的源码么。


还不会用安装boost....能贴下源码吗?
[/Quote]
www.boost.org
asmilemyself 2011-11-11
  • 打赏
  • 举报
回复
菜鸟表示嵌套好复杂
fallening 2011-11-11
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 akirya 的回复:]

引用 24 楼 mingliang1212 的回复:

typedef [](test& x){ x.func() ; } myfun;
myfun(x);

这个貌似不能typedef的。
[/Quote]
再套一层 decltype 差不多就可以 typedef 了
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 mingliang1212 的回复:]

typedef [](test& x){ x.func() ; } myfun;
myfun(x);
[/Quote]
这个貌似不能typedef的。
luciferisnotsatan 2011-11-11
  • 打赏
  • 举报
回复
不清楚,看看别人知道不
  • 打赏
  • 举报
回复
mem_fun应该会内联的
release下试试看。
caddor2011 2011-11-11
  • 打赏
  • 举报
回复
看不懂, 帮忙顶贴
iamnobody 2011-11-11
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 taodm 的回复:]
那不就是抄抄boost::bind的源码么。
[/Quote]

还不会用安装boost....能贴下源码吗?
fallening 2011-11-11
  • 打赏
  • 举报
回复
楼主问什么不用 std::bind 或者 boost::bind ?
pengzhixi 2011-11-11
  • 打赏
  • 举报
回复
额 模板参数的演绎只对模板函数不对模板类。这就意味着对模板类的模板参数来说你必须在实例化的时候显示给出。
taodm 2011-11-11
  • 打赏
  • 举报
回复
那不就是抄抄boost::bind的源码么。
iamnobody 2011-11-11
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 akirya 的回复:]
引用 21 楼 mingliang1212 的回复:

引用 20 楼 akirya 的回复:


只有 call ?func@test@@QAEXXZ ; test::func


我的意思是,test::func都不应该有:
C/C++ code


int main()
{
test x[3]={test(1),test(2),test(3)};
std::fo……
[/Quote]





虽然很不合题意,想问个以前想知道的问题》
想请教一下:

typedef [](test& x){ x.func() ; } myfun;
myfun(x);
怎么样实现这个。。。。
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 mingliang1212 的回复:]

引用 20 楼 akirya 的回复:


只有 call ?func@test@@QAEXXZ ; test::func


我的意思是,test::func都不应该有:
C/C++ code


int main()
{
test x[3]={test(1),test(2),test(3)};
std::for_each( x , x + 3 , std::m……
[/Quote]

用lambda吧,刚才试了一下VC10 G++4.5都能内联
std::for_each( x , x + 3 , [](test& x){ x.func() ; } );
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 mingliang1212 的回复:]

引用 20 楼 akirya 的回复:


只有 call ?func@test@@QAEXXZ ; test::func


我的意思是,test::func都不应该有:
C/C++ code


int main()
{
test x[3]={test(1),test(2),test(3)};
std::for_each( x , x + 3 , std::m……
[/Quote]
了解了,木有其他办法了。
iamnobody 2011-11-11
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 akirya 的回复:]


只有 call ?func@test@@QAEXXZ ; test::func

[/Quote]
我的意思是,test::func都不应该有:


int main()
{
test x[3]={test(1),test(2),test(3)};
std::for_each( x , x + 3 , std::mem_fun_ref(&test::func) );

std::for_each(x,x + 3,my::member_fun<test,void,&test::func>());

system("pause");
return 0;
}





std::for_each( x , x + 3 , std::mem_fun_ref(&test::func) );
008D1047 lea esi,[ebp-10h]
008D104A lea ebx,[ebx]
008D1050 mov ecx,esi
008D1052 call test::func (8D1000h)
008D1057 add esi,4
008D105A lea eax,[ebp-4]
008D105D cmp esi,eax
008D105F jne main+30h (8D1050h)

std::for_each(x,x + 3,my::member_fun<test,void,&test::func>());
008D1061 mov edi,dword ptr [__imp__printf (8D209Ch)]
008D1067 lea esi,[ebp-10h]
008D106A lea ebx,[ebx]
008D1070 mov ecx,dword ptr [esi]
008D1072 push ecx
008D1073 push offset string "%d\n" (8D20F4h)
008D1078 call edi
008D107A add esi,4
008D107D lea edx,[ebp-4]
008D1080 add esp,8
008D1083 cmp esi,edx
008D1085 jne main+50h (8D1070h)


  • 打赏
  • 举报
回复
[Quote=引用 17 楼 mingliang1212 的回复:]

引用 13 楼 akirya 的回复:
mem_fun应该会内联的
release下试试看。
还是不会,因为听说他拒绝给指针变量内联。。。虽然他知道这是个常数。。。
[/Quote]
你搞错了吧。

#include<stdio.h>
#include<functional>
#include<algorithm>

class test
{
int m;
public:
test( int x ):m(x)
{
}

void func()
{
m;
printf("%d\n",m);
}

};

int main()
{
test x[3]={test(1),test(2),test(3)};
std::for_each( x , x + 3 , std::mem_fun_ref(&test::func) );

return 0;
}

编译选项
cl file.cpp /O2 /FAs
输出汇编代码

PUBLIC _main
; Function compile flags: /Ogtpy
; File c:\documents and settings\hwp\桌面\test.cpp
; COMDAT _main
_TEXT SEGMENT
_x$ = -12 ; size = 12
_main PROC ; COMDAT

; 22 : {

sub esp, 12 ; 0000000cH
push esi

; 23 :
; 24 : test x[3]={test(1),test(2),test(3)};

mov DWORD PTR _x$[esp+16], 1
mov DWORD PTR _x$[esp+20], 2
mov DWORD PTR _x$[esp+24], 3

; 25 :
; 26 : std::for_each( x , x + 3 , std::mem_fun_ref(&test::func) );

lea esi, DWORD PTR _x$[esp+16]
$LL15@main:
mov ecx, esi
call ?func@test@@QAEXXZ ; test::func
add esi, 4
lea eax, DWORD PTR _x$[esp+28]
cmp esi, eax
jne SHORT $LL15@main

; 27 :
; 28 :
; 29 : return 0;

xor eax, eax
pop esi

; 30 : }

add esp, 12 ; 0000000cH
ret 0
_main ENDP
_TEXT ENDS
END

只有 call ?func@test@@QAEXXZ ; test::func
加载更多回复(9)

65,176

社区成员

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

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