2

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!

Christian G
  • 943
  • 9
  • 16
  • @Someprogrammerdude return type: pre-increment return T&, post-increment return T and signature: https://en.cppreference.com/w/cpp/language/operator_incdec – Christian G Nov 09 '18 at 13:59
  • Your type becomes `decltype(&T::operator++);`, or `T& (T::*)()`. – llllllllll Nov 09 '18 at 14:02
  • You can define a postfix increment operator that returns a reference, though: [example](http://coliru.stacked-crooked.com/a/615e2bb099cb7585). – Nelfeal Nov 09 '18 at 14:02

1 Answers1

2

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

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

Nelfeal
  • 12,593
  • 1
  • 20
  • 39