enable_if + disable_if组合将产生一个暧昧电话 [英] enable_if + disable_if combination provokes an ambiguous call
问题描述
在试图回答这个问题我想建议使用 enable_if
+ disable_if
允许的方法基于这样的事实,一个类型是过载(或没有)多态。
While trying to answer this question I wanted to suggest the use of enable_if
+ disable_if
to allow the overload of a method based on the fact that a type was (or not) polymorphic.
所以,我创建了一个小测试文件:
So I created a small test file:
template <class T>
void* address_of(T* p,
boost::enable_if< boost::is_polymorphic<T> >* dummy = 0)
{ return dynamic_cast<void*>(p); }
template <class T>
void* address_of(T* p,
boost::disable_if< boost::is_polymorphic<T> >* dummy = 0)
{ return static_cast<void*>(p); }
struct N { int x; };
int main(int argc, char* argv[])
{
N n;
std::cout << address_of(&n) << std::endl;
return 0;
}
这似乎很温顺。
但是GCC(3.4 ...)这个呛:
However gcc (3.4 ...) choke on this:
TEST.CPP:在功能 INT主(INT,CHAR **)
:结果
TEST.CPP:29:错误:重载 address_of(N *)
是模糊的结果呼叫
TEST.CPP:17:注意:考生:无效* address_of(T *
[与T = N]结果
提高:: enable_if&LT;提高:: is_polymorphic&LT; T&GT ;,无效&GT; *)
TEST.CPP:20:注意:无效* address_of(T *
[与T = N]
提高:: disable_if&LT;提高:: is_polymorphic&LT; T&GT ;,无效&GT; *)
test.cpp: In function
int main(int, char**)
:
test.cpp:29: error: call of overloadedaddress_of(N*)
is ambiguous
test.cpp:17: note: candidates are:void* address_of(T*, boost::enable_if<boost::is_polymorphic<T>, void>*)
[with T = N]
test.cpp:20: note:void* address_of(T*, boost::disable_if<boost::is_polymorphic<T>, void>*)
[with T = N]
这似乎相当清楚我的人的心灵而超载应该在这里使用。我的意思是它似乎很清楚,我已经定义了一个替代方案,只有一个功能,可以在同一时间内使用的......我本来以为SFINAE将采取无效不必要的超负荷的照顾。
It seems rather clear to my human mind which overload should be used here. I mean it seems clear that I have defined an alternative and only one function can be used at a time... and I would have thought that SFINAE would take care of invalidating the unnecessary overload.
我修补它通过使用 ...
(省略号)代替 disable_if
并需要一个虚拟的第二个参数......但我在为什么编译器噎死这仍然感兴趣。
I patched it up by using ...
(ellipsis) instead of disable_if
and requiring a dummy second argument... but I am still interested in why the compiler choke on this.
推荐答案
哽咽,因为你忘了尾随 ::类型
编译器 enable_if
和 disable_if
。模板始终定义;这只是该成员键入
是present当且仅当恩pression是真正
(对于 enable_if
)或假
(为 disable_if
)。
The compiler choked because you forgot the trailing ::type
on enable_if
and disable_if
. The templates are always defined; it is just that the member type
is present if and only if the expression is true
(for enable_if
) or false
(for disable_if
).
template <class T>
void* address_of(T* p,
typename boost::enable_if< boost::is_polymorphic<T> >::type* dummy = 0)
{ return dynamic_cast<void*>(p); }
template <class T>
void* address_of(T* p,
typename boost::disable_if< boost::is_polymorphic<T> >::type* dummy = 0)
{ return static_cast<void*>(p); }
但没有尾随 ::类型
,你的函数模板只是创建需要指向 enable_if
的实例或过载 disable_if
作为第二个参数。随着尾随 ::类型
,模板可以创建一个重载型无效*
,或第二参数过载消除(即所希望的行为)。
Without the trailing ::type
, your function templates just create overloads that take pointers to instances of enable_if
or disable_if
as the second parameter. With the trailing ::type
, the templates either create an overload with a second parameter of type void*
, or the overload is removed (i.e. the desired behaviour).
这篇关于enable_if + disable_if组合将产生一个暧昧电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!