为什么以下 SFINAE 测试无法检测到模板成员函数?
使用 GCC 进行编译时,我从以下代码中得到的总是 false。我相信这是一个编译器错误,但有人可能更了解。
#include <iostream>
template< class T >
class has_apply {
typedef char yes[1];
typedef char no[2];
template< class U, U u >
struct binder {};
template< class U, unsigned n >
static yes& test( U*,
binder< void (U::*) ( const double& ),
&U::template apply< n >
>* = 0
);
template< class U, unsigned n >
static no& test( ... );
public:
static const bool result =
( sizeof( yes ) == sizeof( test< T, 0u >( (T*)(0) ) ) );
};
class A {
public:
template< unsigned n >
void apply( const double& );
};
int main()
{
std::cout << std::boolalpha << has_apply< A >::result << '\n';
return( 0 );
}
compiling with GCC i get always false from the following code. I believe this is a compiler bug, but someone may know better.
#include <iostream>
template< class T >
class has_apply {
typedef char yes[1];
typedef char no[2];
template< class U, U u >
struct binder {};
template< class U, unsigned n >
static yes& test( U*,
binder< void (U::*) ( const double& ),
&U::template apply< n >
>* = 0
);
template< class U, unsigned n >
static no& test( ... );
public:
static const bool result =
( sizeof( yes ) == sizeof( test< T, 0u >( (T*)(0) ) ) );
};
class A {
public:
template< unsigned n >
void apply( const double& );
};
int main()
{
std::cout << std::boolalpha << has_apply< A >::result << '\n';
return( 0 );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不能声称理解为什么,但我能够通过不采用 U* 并拉出绑定器类型的声明来使您的代码工作:
您实际上可以通过从函数中删除无符号参数来简化这一点,并且只需在“declare”内的 typedef 中粘贴 0u 即可。
再说一遍,我无法解释为什么这个中间元函数是必要的,但它是必需的,并且上述内容在 MSVC++ 2010 中有效
I can't claim to understand why, but I was able to make your code work by not taking U* and by pulling the declaration of the binder type out:
You can actually simplify this a bit by removing the unsigned parameter from the function and just sticking 0u in the typedef within 'declare'.
Again, I can't explain why this intermediate metafunction is necessary but it was required and the above works in MSVC++ 2010
Andy Venikov 在 [comp.lang.c++.moderated] 中的回答(我只是将功劳归功于伟大的 google-foo(呵呵,我作弊了)):
http://groups.google.com/group/comp.lang.c++.moderated/msg/93017cf706e08c9e
Andy Venikov's answer over in [comp.lang.c++.moderated] (I'm only taking credit for great google-foo (he he, I cheated)):
http://groups.google.com/group/comp.lang.c++.moderated/msg/93017cf706e08c9e
就像诺亚一样,我不知道为什么。与 Noah 不同,我没有找到可行的解决方案,但调查了我设法使 MingW g++ 4.4.1 编译器崩溃的事情(即内部编译器错误)。这只是通过不一致地将
apply
引用为模板和非模板:对 g++ 的影响:
呵呵...
PS:我很乐意将此作为“评论”发布,因为它不是一个“回答”。
Like Noah I don't know why. Unlike Noah I didn't find a workable solution, but investigating the thing I managed to crash the MingW g++ 4.4.1 compiler (that is, an Internal Compiler Error). This was simply by inconsistently referring to
apply
as template and non-template:Effect on g++:
He he...
PS: I'd love to post this as a "comment", since it's not an "answer".
这并不是它不起作用的原因的答案。然而,通过网络研究,我找到了一些示例,并最终得到了以下代码,这可能比我一直在尝试的更切题。
我试图检测特定的成员函数签名,但下面的代码超出了范围,检测给定的调用是否可能,无论签名是什么。希望评论能有所帮助。
This is not an answer to why it doesn't work. However, researching through the web, I've found some examples and eventually got to the following code, which may be even more to the point then what I've been trying.
I was trying to detect an specific member function signature, but the code below goes beyond and detects whether a given call is possible, no matter what is the signature. Hope the comments will be helpful.