繁体   English   中英

使用 std::is_detected_exact 检测 operator++ 签名

[英]Detect operator++ signature with std::is_detected_exact

我想在编译时检测给定类型是否具有带有库基础知识 TS v2 type_traits' is_detected_exact 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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM