Can't compile suggested atomic_fetch_max code

53 Views Asked by At

During code review of my demo, reviewer suggested to use atomic_fetch_max compatible with the proposal Atomic maximum/minimum:

template<typename T>
T atomic_fetch_max(std::atomic<T>* obj, typename std::atomic<T>::value_type arg) noexcept
{
    auto prev = *obj;
    while (prev < arg && !obj->compare_exchange_weak(prev, arg)) {}
    return prev;
}

instead of my code

template<typename T>
void update_max(std::atomic<T>& max_value, T const& value) noexcept
{
    T prev_value = max_value;
    while (prev_value < value && 
           !max_value.compare_exchange_weak(prev_value, value))
    {}
}

I tried to incorporate it into my demo and can't make it compiled. Please see the demo.

Two questions here related to the problem:

  1. Why the suggested version fails to compile? I can't get the reason from compiler messages (they are too long and I am not sure how to include them here, please update the question if you know the format, I will follow).
  2. What is the reason the updated version uses typename std::atomic<T>::value_type arg instead of plain T or even better T const & which makes arg referenced value const?
0

There are 0 best solutions below