Вопрос

This is a trivial question about decltype... I have something like this:

using namespace std;
template< typename BIt >
void dostuff(BIt first, BIt middle, BIt last)
{
    auto temp = *first;
    for_each(first, middle, [](decltype((temp)) el)
    {cout << el << " "; });
}

int main()
{
    std::vector<int> vi{11,22,33,44,1,2,3,4,5};
    dostuff(std::begin(vi), std::begin(vi) + 4, std::end(vi));
}

How do I avoid usage of temp variable(besides being ugly IDK if dereferencing .beginan of an empty vector is legal :) )?

Это было полезно?

Решение

You might be looking for the (much more verbose, mind you) std::iterator_traits.

In particular, this resolves to typename std::iterator_traits<BIt>::value_type.

I don't think there's much benefit to using it though, it's more verbose and relies on an extra include, <iterator>. If you didn't want the temp variable you could very well just use decltype(*first).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top