C++ - decltype example

less than 1 minute read

The following code is trying to make a reference to avoid the copy

1
auto& element = *pos;

then we will always receive a reference to the element, but the program will fail if the iterator’s operator* returns a value. To address this problem, we can use decltype so that the value- or reference-ness of the iterator’s operator* is preserved:

1
decltype(*pos) element = *pos;

or

1
decltype(auto) element = *pos;

Categories:

Updated: