gpt4 book ai didi

c++ - 为什么这个用于检测类型 T 是否具有 void operator(EDT const&) 的 C++ 特性会失败?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:51:00 25 4
gpt4 key购买 nike

我正在尝试使用 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

我预计:真假假假假假假

完成的工作:

  1. 阅读此 Stackoverflow question .我也关注了相关链接。

  2. 查找 std::declval、decltype。我还研究了一些关于非类型模板参数如何导致歧义的问题。我一直在使用 http://en.cppreference.com/主要是。

  3. 阅读其他一些相关问题和链接。

注意:我正在使用 C++ 0x 开发 gcc 4.6.3。

最终目标:检测此签名中包含的所有可调用函数指针。

相关说明: 我对一些概念仍然感到困惑,如果您能回答这些问题,我将不胜感激。当然,如果它们属于单独的问题,请告诉我。

  1. 对于这种情况,我们可以通过使用 declval 而不是 Check 模板来触发 SFINAE 的歧义吗?

  2. 重载运算符的函数类型是什么?例如,这种情况下的重载运算符是否具有以下类型:void (operator())(EDT const&)?

  3. 由于在此检查期间丢弃了 CV 限定符,因此检查我传递的参数的常量性的最佳方法是什么。\

  4. 我还没有想出使用 Boost 来执行此操作的方法。我还坚持使用较旧的 Boost 版本 1.43(将检查并更新确切的版本),我相信。如果没有理由自己开支票,那可能是最好的选择。

我在这方面仍然是一个初学者,如果这太基础了,我深表歉意。如果您能指出您认为我也应该关注的其他资源,我将不胜感激。与此同时,我会继续在线搜索并尝试解决方案。


编辑 1


在与@Nir Friedman 讨论这个问题后,我开始意识到打破隐式转换规则并实现精确匹配并不是我想要的。只要可以转换传递的类型,就应该没问题。我将不胜感激关于如何实现这一点的指导。


编辑 2


我将此问题标记为已关闭,因为@Sam Varshavchik 回答了我提出的确切问题。如果有人对 EDIT 1 中提出的问题感兴趣,我会把它作为一个单独的问题提出,或者在此处发布我的解决方案以供引用。

最佳答案

您阐明了您的 operator() 返回一个 void,并且您希望严格匹配签名,忽略类型转换。

如果是这样,那么您的预期结果应该是 false true false false false false,而不是 true false false false false false:

 is_callable_oper<has, int&>::value

由于 has::operator() 没有采用 int & const & 参数,它折叠为 int &,结果这个测试一定是假的。

 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 {};

关于c++ - 为什么这个用于检测类型 T 是否具有 void operator(EDT const&) 的 C++ 特性会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42583014/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com