为什么这个用于检测类型 T 是否具有 void operator(EDT const&) 的 C++ 特性会失败?
Why does this C++ trait to detect if a type T has a void operator(EDT const&) fail?
我正在尝试使用 SFINAE 检测作为模板参数 T 传递的类型是否具有 T::operator()(P const&),其中 P 也是模板参数。我根据 Member Detector Idiom 的这个示例对我的解决方案进行了建模,不幸的是,我无法让它为 operator() 工作,即使我可以让它为普通方法工作。
下面是一些演示我面临的问题的示例代码:
#include <iostream>
#include <iomanip>
#include <utility>
#include <type_traits>
using namespace std;
struct has
{
void operator()(int const&);
};
struct hasNot1
{
void operator()(int);
};
struct hasNot2
{
void operator()();
};
struct hasNot3
{
void operator()(float);
};
struct hasNot4
{
};
template<typename T, typename EDT>
struct is_callable_oper
{
private:
typedef char(&yes)[1];
typedef char(&no)[2];
template <typename U, void (U::*)(EDT const &)> struct
Check;
template<typename>
static yes test(...);
template <typename U>
static no
test(Check<U, &U::operator()>*);
public:
static constexpr bool value = sizeof(test<T>(0))
== sizeof(yes);
};
int main() {
cout << boolalpha << is_callable_oper<has, int&>::value << " "
<< is_callable_oper<has, int>::value << " "
<< is_callable_oper<hasNot1, int&>::value << " "
<< is_callable_oper<hasNot2, int&>::value << " "
<< is_callable_oper<hasNot3, int&>::value << " "
<< is_callable_oper<hasNot4, int&>::value << endl;
return 0;
}
运行 它在 ideone (https://ideone.com/tE49xR) 上产生:
true false true true true true
我预计:
true false false false false false
完成的工作:
阅读此 Whosebug question。我也关注了相关链接。
查找 std::declval,decltype。我还研究了一些关于非类型模板参数如何导致歧义的问题。我一直主要使用 http://en.cppreference.com/。
阅读其他一些相关问题和链接。
注意:我正在使用 C++ 0x 开发 gcc 4.6.3。
最终目标: 检测此签名中包含的所有可调用函数指针。
相关说明: 有些概念我还是比较迷糊,请您解答。当然,如果它们属于单独的问题,请告诉我。
对于这种情况,我们可以通过使用 declval 而不是 Check 模板来触发 SFINAE 的歧义吗?
重载运算符的函数类型是什么?例如,这种情况下的重载运算符是否具有以下类型:void (operator())(EDT const&)
?
由于在此检查期间丢弃了 CV 限定符,因此检查我传递的参数的常量性的最佳方法是什么。\
我还没想出使用 Boost 来完成此操作的方法。我还坚持使用较旧的 Boost 版本 1.43(将检查并更新确切的版本),我相信。如果没有理由自己开支票,那可能是最好的选择。
我在这方面仍然是一个初学者,如果这太基础了,我深表歉意。如果您能指出您认为我也应该关注的其他资源,我将不胜感激。同时,我会继续在网上搜索并尝试解决方案。
编辑 1
在与@Nir Friedman 讨论过这个问题后,我开始意识到打破隐式转换规则并实现精确匹配并不是我想要的。只要可以转换传递的类型,就应该没问题。我将不胜感激关于如何实现这一目标的指导。
编辑 2
我将此问题标记为已关闭,因为@Sam Varshavchik 回答了我提出的确切问题。如果有人对 EDIT 1 中提出的问题感兴趣,我会把它作为一个单独的问题提出,或者 post 我的解决方案在这里供参考。
在更现代的编译器上,解决此类问题的典型方法是 void_t
惯用语:How does `void_t` work。事情是这样的。第 1 步:
template <class T>
using void_t = void; // avoid variadics for simplicity
显然这是一般性的,并不特定于您的问题,但它直到 17 才恰好出现在标准库中。
接下来,我们定义特征并将默认检测设为假:
template <class T, class P, class = void>
struct is_callable_oper : std::false_type {};
现在,我们定义了一个专门的形式,如果我们的函数有我们想要的运算符,它就会起作用。
template <class T, class P>
struct is_callable_oper<T, P,
void_t<decltype(std::declval<T>()(std::declval<P>()))>>: std::true_type {};
基本思想是我们知道 void_t
将始终转换为 void
。但是,除非表达式有效,否则输入的类型将没有任何意义;否则会导致替换失败。所以基本上,上面的代码将检查是否
std::declval<T>()(declval<P>())
适合您的类型。这基本上就是您想要的特征检测。请注意,有一些涉及左值、右值、常量的微妙之处,但要深入到这里会有点牵扯。
现在,正如 Sam 在评论中指出的那样,此表达式是否合理包括隐式转换。所以输出确实是真真真假真假。工作示例:http://coliru.stacked-crooked.com/a/81ba8b208b90a2ba.
例如,您预计 is_callable_oper<has, int>::value
为假。然而,显然可以调用一个接受 const int&
和 int
的函数。所以相反,你是真的。
您阐明了您的 operator()
returns 和 void
,并且您希望严格匹配签名,忽略类型转换。
如果是这样,那么您的预期结果应该是 false true false false false false
,而不是 true false false false false false
:
is_callable_oper<has, int&>::value
由于 has::operator()
没有采用 int & const &
参数,它会折叠为 int &
,因此此测试的结果必须为 false。
is_callable_oper<has, int>
因为 has
确实有一个 operator()
需要一个 const int &
参数,这个测试应该通过。
我的解决方案只是使用 std::is_same
比较两种类型,并使用 std::enable_if
使 SFINAE 无法通过模板解析候选。
#include <type_traits>
#include <iostream>
struct has
{
void operator()(int const&);
};
struct hasNot1
{
void operator()(int);
};
struct hasNot2
{
void operator()();
};
struct hasNot3
{
void operator()(float);
};
struct hasNot4
{
};
template<typename T, typename P, typename foo=void>
class is_callable_oper : public std::false_type {};
template<typename T, typename P>
class is_callable_oper<T, P, typename std::enable_if<
std::is_same<decltype(&T::operator()),
void (T::*)(const P &)>::value>::type>
: public std::true_type {};
int main() {
std::cout << std::boolalpha << is_callable_oper<has, int&>::value << " "
<< is_callable_oper<has, int>::value << " "
<< is_callable_oper<hasNot1, int&>::value << " "
<< is_callable_oper<hasNot2, int&>::value << " "
<< is_callable_oper<hasNot3, int&>::value << " "
<< is_callable_oper<hasNot4, int&>::value << std::endl;
return 0;
}
编辑:通过使用 std::void_t
或合理的传真,在专业化中也应该可以使它与重载运算符一起工作:
template<typename T, typename P>
class is_callable_oper<T, P,
std::void_t<decltype( std::declval< void (T::*&)(const P &)>()=&T::operator())>>
: public std::true_type {};
我正在尝试使用 SFINAE 检测作为模板参数 T 传递的类型是否具有 T::operator()(P const&),其中 P 也是模板参数。我根据 Member Detector Idiom 的这个示例对我的解决方案进行了建模,不幸的是,我无法让它为 operator() 工作,即使我可以让它为普通方法工作。
下面是一些演示我面临的问题的示例代码:
#include <iostream>
#include <iomanip>
#include <utility>
#include <type_traits>
using namespace std;
struct has
{
void operator()(int const&);
};
struct hasNot1
{
void operator()(int);
};
struct hasNot2
{
void operator()();
};
struct hasNot3
{
void operator()(float);
};
struct hasNot4
{
};
template<typename T, typename EDT>
struct is_callable_oper
{
private:
typedef char(&yes)[1];
typedef char(&no)[2];
template <typename U, void (U::*)(EDT const &)> struct
Check;
template<typename>
static yes test(...);
template <typename U>
static no
test(Check<U, &U::operator()>*);
public:
static constexpr bool value = sizeof(test<T>(0))
== sizeof(yes);
};
int main() {
cout << boolalpha << is_callable_oper<has, int&>::value << " "
<< is_callable_oper<has, int>::value << " "
<< is_callable_oper<hasNot1, int&>::value << " "
<< is_callable_oper<hasNot2, int&>::value << " "
<< is_callable_oper<hasNot3, int&>::value << " "
<< is_callable_oper<hasNot4, int&>::value << endl;
return 0;
}
运行 它在 ideone (https://ideone.com/tE49xR) 上产生:
true false true true true true
我预计:
true false false false false false
完成的工作:
阅读此 Whosebug question。我也关注了相关链接。
查找 std::declval,decltype。我还研究了一些关于非类型模板参数如何导致歧义的问题。我一直主要使用 http://en.cppreference.com/。
阅读其他一些相关问题和链接。
注意:我正在使用 C++ 0x 开发 gcc 4.6.3。
最终目标: 检测此签名中包含的所有可调用函数指针。
相关说明: 有些概念我还是比较迷糊,请您解答。当然,如果它们属于单独的问题,请告诉我。
对于这种情况,我们可以通过使用 declval 而不是 Check 模板来触发 SFINAE 的歧义吗?
重载运算符的函数类型是什么?例如,这种情况下的重载运算符是否具有以下类型:
void (operator())(EDT const&)
?由于在此检查期间丢弃了 CV 限定符,因此检查我传递的参数的常量性的最佳方法是什么。\
我还没想出使用 Boost 来完成此操作的方法。我还坚持使用较旧的 Boost 版本 1.43(将检查并更新确切的版本),我相信。如果没有理由自己开支票,那可能是最好的选择。
我在这方面仍然是一个初学者,如果这太基础了,我深表歉意。如果您能指出您认为我也应该关注的其他资源,我将不胜感激。同时,我会继续在网上搜索并尝试解决方案。
编辑 1
在与@Nir Friedman 讨论过这个问题后,我开始意识到打破隐式转换规则并实现精确匹配并不是我想要的。只要可以转换传递的类型,就应该没问题。我将不胜感激关于如何实现这一目标的指导。
编辑 2
我将此问题标记为已关闭,因为@Sam Varshavchik 回答了我提出的确切问题。如果有人对 EDIT 1 中提出的问题感兴趣,我会把它作为一个单独的问题提出,或者 post 我的解决方案在这里供参考。
在更现代的编译器上,解决此类问题的典型方法是 void_t
惯用语:How does `void_t` work。事情是这样的。第 1 步:
template <class T>
using void_t = void; // avoid variadics for simplicity
显然这是一般性的,并不特定于您的问题,但它直到 17 才恰好出现在标准库中。
接下来,我们定义特征并将默认检测设为假:
template <class T, class P, class = void>
struct is_callable_oper : std::false_type {};
现在,我们定义了一个专门的形式,如果我们的函数有我们想要的运算符,它就会起作用。
template <class T, class P>
struct is_callable_oper<T, P,
void_t<decltype(std::declval<T>()(std::declval<P>()))>>: std::true_type {};
基本思想是我们知道 void_t
将始终转换为 void
。但是,除非表达式有效,否则输入的类型将没有任何意义;否则会导致替换失败。所以基本上,上面的代码将检查是否
std::declval<T>()(declval<P>())
适合您的类型。这基本上就是您想要的特征检测。请注意,有一些涉及左值、右值、常量的微妙之处,但要深入到这里会有点牵扯。
现在,正如 Sam 在评论中指出的那样,此表达式是否合理包括隐式转换。所以输出确实是真真真假真假。工作示例:http://coliru.stacked-crooked.com/a/81ba8b208b90a2ba.
例如,您预计 is_callable_oper<has, int>::value
为假。然而,显然可以调用一个接受 const int&
和 int
的函数。所以相反,你是真的。
您阐明了您的 operator()
returns 和 void
,并且您希望严格匹配签名,忽略类型转换。
如果是这样,那么您的预期结果应该是 false true false false false false
,而不是 true false false false false false
:
is_callable_oper<has, int&>::value
由于 has::operator()
没有采用 int & const &
参数,它会折叠为 int &
,因此此测试的结果必须为 false。
is_callable_oper<has, int>
因为 has
确实有一个 operator()
需要一个 const int &
参数,这个测试应该通过。
我的解决方案只是使用 std::is_same
比较两种类型,并使用 std::enable_if
使 SFINAE 无法通过模板解析候选。
#include <type_traits>
#include <iostream>
struct has
{
void operator()(int const&);
};
struct hasNot1
{
void operator()(int);
};
struct hasNot2
{
void operator()();
};
struct hasNot3
{
void operator()(float);
};
struct hasNot4
{
};
template<typename T, typename P, typename foo=void>
class is_callable_oper : public std::false_type {};
template<typename T, typename P>
class is_callable_oper<T, P, typename std::enable_if<
std::is_same<decltype(&T::operator()),
void (T::*)(const P &)>::value>::type>
: public std::true_type {};
int main() {
std::cout << std::boolalpha << is_callable_oper<has, int&>::value << " "
<< is_callable_oper<has, int>::value << " "
<< is_callable_oper<hasNot1, int&>::value << " "
<< is_callable_oper<hasNot2, int&>::value << " "
<< is_callable_oper<hasNot3, int&>::value << " "
<< is_callable_oper<hasNot4, int&>::value << std::endl;
return 0;
}
编辑:通过使用 std::void_t
或合理的传真,在专业化中也应该可以使它与重载运算符一起工作:
template<typename T, typename P>
class is_callable_oper<T, P,
std::void_t<decltype( std::declval< void (T::*&)(const P &)>()=&T::operator())>>
: public std::true_type {};