Std :: result_of applies to overloaded const methods

If i give

typedef std::vector<int> v;

Then it can be used below to write the type of constant iterator (an alternative is to use v::const_iterator, but it depends on the type of element const_iteratorthat is explicitly defined in the class.

typedef typename std::result_of<decltype(&v::cbegin)(v*)>::type const_iterator;

In fact, we can verify that the above does the way we want.

static_assert(std::is_same<const_iterator, typename v::const_iterator>::value);

However, I found a compiler error below.

typedef typename std::result_of<decltype(&v::begin)(v*)>::type iterator;

The compiler complains that the method is overloaded (const modifier) ​​and cannot be unambiguously resolved. However, I cannot find the syntax to eliminate the ambiguity. At a minimum, we expect it to be unambiguous below, because only the const version can work on a const object. However, even the following is likewise problematic.

typedef typename std::result_of<decltype(&v::begin)(const v*)>::type const_iterator2;

How can I refer to a specific version of const or nonconst for starters?

+4
1

, :

using v = std::vector<int>;
using iter = decltype(std::declval<v>().begin());
static_assert(std::is_same<iter, typename v::iterator>::value);

, &v::begin . v::begin, & , . std::declval . std::declval<v>() v, , v::begin().

, const:

using citer = decltype(std::declval<const v>().begin());
static_assert(std::is_same<citer, typename v::contst_iterator>::value);

, . std::declval , , decltype.

+4

Source: https://habr.com/ru/post/1667275/


All Articles