c++ - 使用 std::is_Detected_exact 检测运算符++签名

标签 c++ type-traits fundamentals-ts

我想在编译时检测给定类型是否具有库基础 TS v2 type_traits' is_detected_exact 的预增量运算符。 helper - 但是,看来我要么误解了这个helper,要么提供了错误的参数,以下代码无法编译:

#include <experimental/type_traits>

template<typename T>
using operator_plusplus_t = decltype(&T::operator++);

template<typename T>
using has_pre_increment = std::experimental::is_detected_exact<T&, operator_plusplus_t, T>;

struct incrementer
{
  incrementer& operator++() { return *this; };
};

static_assert(has_pre_increment<incrementer>::value, "type does not have pre increment");

我得到的错误是这个(static_assert 失败):

<source>:14:15: error: static assertion failed: type does not have pre increment  
static_assert(has_pre_increment<incrementer>::value, "type does not have pre increment");
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compiler returned: 1

https://godbolt.org/z/-zoUd9

我期望这段代码能够编译,因为“incrementer”结构有一个operator++方法,没有参数返回对其类型的引用...

也许您可以为我指明正确的方向,提前致谢!

最佳答案

您可以使用decltype(++std::declval<T>())相反。

https://godbolt.org/z/h_INw-

关于c++ - 使用 std::is_Detected_exact 检测运算符++签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53226919/

相关文章:

c++ - 简单的继承问题

c++ - 媒体基金会 AMR 解码

c++ - Windows 上 DLL Exporting/Importing 和 Extern 的问题

c++ - 带有 stdenable_if 的嵌套名称说明符中使用的不完整类型

c++ - 如何在模板中同时允许浮点和整数类型但不允许 bool

c++ - 如何检查 C++ 中的类型 `T` 是否为 `std::pair<?, bool>`?

c++ - 在 A* 遍历后从 map 中移除产生最佳路径的障碍

c++ - 没有运算符 "*"匹配这些操作数

c++ - std::experimental::source_location 如何实现?