简体   繁体   English

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

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

I want to detect at compile time if a given type has the pre-increment operator with the library fundamentals TS v2 type_traits' is_detected_exact helper - however, it seems like I either misunderstood this helper or I supplied the wrong parameters, the following code does not compile:我想在编译时检测给定类型是否具有带有库基础知识 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");

The error I get is this one (the static_assert fails):我得到的错误是这个(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 https://godbolt.org/z/-zoUd9

I was expecting this code to compile since the "incrementer" struct has a operator++ method with no arguments returning a reference to its type ...我期待这段代码能够编译,因为“增量器”结构有一个 operator++ 方法,没有参数返回对其类型的引用......

Maybe you can point me in the right direction, thanks in advance!也许您可以指出我正确的方向,提前致谢!

You can use decltype(++std::declval<T>()) instead.您可以使用decltype(++std::declval<T>())代替。

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

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

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