c++type-traitsfundamentals-ts

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:

#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):

<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

I was expecting this code to compile since the "incrementer" struct has a operator++ method with no arguments returning a reference to its type ...

Maybe you can point me in the right direction, thanks in advance!


Solution

  • You can use decltype(++std::declval<T>()) instead.

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