再续:增强boost中function_traits的能力

关于这个问题,John Maddock老大在新闻组里面给出了很详细的解答,虽然我还有些问题要问,不过得好好组织一下怎么说才行……可不要浪费网络资源哈~
主要意思是,对于成员函数traits而言,一方面有const和volatile的麻烦事情,另一方面又有一个隐含的this参数不好处理(究竟是算一个入口参数还是不算?),最重要的是这种应用需求完全可以用其他方式替代。所以,John认为没有必要实现这样的功能。

回信全文如下:
From: John Maddock <john <at> johnmaddock.co.uk>
Subject: Re: [type_traits]Member function traits in function_traits?
Newsgroups: gmane.comp.lib.boost.devel
Date: 2006-09-11 17:41:01 GMT (12 hours and 41 minutes ago)
Realdodo Du wrote:
> I'm not sure the meaning of "wider questions". Could anyone tell me
> what is the question and the differences between global function
> pointers and member function pointers, please?

OK let me try and explain:

Function traits only recognises function types, for example given:

typedef int ft(int);

ft is a function type whose type is "int (int)", you can declare a function
with it:

ft myproc; // declares int myproc(int)

but that's about it.

In contrast a function pointer:

int (*)(int)

or a function reference:

int (&)(int)

can *not* be passed to function_traits, but you can always use
remove_pointer or remove_reference respectively to convert them to something
that you can.

Now, for member-functions there is no such type as "member function" only
pointers to member functions, for example:

int (myclass::*)(int)

You cannot pass such a type to function_traits, and you can't use
remove_pointer to convert it to something that you can either.

Now... if you try and extend function_traits to deal with
member-function-pointers the you get all kinds of tricky questions, like
"what is the functions arity?" - does the hidden "this" pointer count in
other words.

Then you have to decide what to do about const or volatile qualified
functions etc.

So... the question might be, why not a remove_member_pointer trait that
converts:

T (U::*) to T

except that no one seems to have thought of let alone needed such a beast
until now.

So.... the follow up question might be, since your code has to handle
member-function-pointers differently from regular function-pointers, why not
change:

template <class T>
void f(T f)
{
typedef function_traits<typename remove_pointer<T>::type> ft;
// use ft here...
}

to

template <class T, class U>
void f(T (U::*))
{
typedef function_traits<T> ft;
// use ft here, T is deduced as a function type
}

which obviously may or may not look anything like your actual code :-)

To conclude, how should this best be handled?

John.

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

<script type="text/javascript"> document.domain = 'gmane.org'; document.title = 'Re: type traits Member function traits in function traits?'; </script>  <iframe frameborder="0" id="gn_notemagic" style="position: absolute; display: block; opacity: 0.7; z-index: 500; width: 16px; height: 20px; top: 84px; right: 401px;" src="http://www.google.com/gn/static_files/blank.html"></iframe>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值