2

C++11 doc defines std::atomic::fetch_or() and std::atomic::fetch_and() only for Integral types. In this way, MSVC++ 2012 std::atomic<bool> is not implements this functions. Does anyone know why?

I found only this solution. Implement specialization for std::atomic_fetch_or<bool> and std::atomic_fetch_or<and>.

namespace std
{
    template<>
    inline bool atomic_fetch_or<bool>(std::atomic<bool>* atom, bool val)
    {
        bool bRes = !val;
        atom->compare_exchange_strong(bRes, true);
        return bRes;
    }

    template<>
    inline bool atomic_fetch_or_explicit<bool>(std::atomic<bool>* atom,
        bool val, std::memory_order order)
    {
        bool bRes = !val;
        atom->compare_exchange_strong(bRes, true, order);
        return bRes;
    }


    template<>
    inline bool atomic_fetch_and<bool>(std::atomic<bool>* atom, bool val)
    {
        bool bRes = true;
        atom->compare_exchange_strong(bRes, val);
        return bRes;
    }

    template<>
    inline bool atomic_fetch_and_explicit<bool>(std::atomic<bool>* atom,
        bool val, std::memory_order order)
    {
        bool bRes = true;
        atom->compare_exchange_strong(bRes, val, order);
        return bRes;
    }
}

Unfortunately it is inconvenient to use as if it were a built-in operators.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
23W
  • 1,413
  • 18
  • 37
  • 1
    `bool` is an integral type... I would venture that MSVC has some bug... – Massa Apr 01 '15 at 12:50
  • 2
    @Massa The standard doesn't say `atomic_fetch_and` (and similar functions) is available for `atomic` where `T` is any integral type. It explicitly lists the types for which it's available, in **29.5 Table 146** and **29.6.5 Table 147**. `atomic` is *not* so listed. – Igor Tandetnik Apr 02 '15 at 00:13

0 Answers0