作者:realdodo 来源:realdodo.com   酷勤网收集 2007-09-13

摘要
  对于成员函数traits而言,一方面有const和volatile的麻烦事情,另一方面又有一个隐含的this参数不好处理,最重要的是这种应用需求完全可以用其他方式替代。

上一篇说过,在function_traits里面加入成员函数类型traits的工作纯粹是体力活,那么原作者没有理由“忽略”这个方面——毕竟他已经做了绝大多数的体力活,这个type_traits库就是大智慧和大工作量“完美结合”的产物。 
那么,why not?就这个问题我专门写信给作者John Maddock,他的回答让我不太理解了:

I don't think this is quite the right approach: function_traits was
deliberately written *not* to work with function-pointers - since you can
always strip the pointer off before passing the type to function_traits -
but handling member function pointers raise some interesting wider
questions
.

为什么会有wider question呢?到底是什么问题呢?嗯,我已经继续提问了,希望得到进一步的解答。

关于这个问题,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

分类: C++名库 编程语言



关于酷勤 | 联系方式 | 免责声明 | 友情链接