Is it possible to detect if a type is atomic using enable_if ?
template<typename TT>
void set(String v, TT &value, std::false_type, typename std::enable_if<std::is_class<TT>::value>::type * = 0)
{
}
Currently, my atomic type is being detected as a class type
is there anyway to distinguish it as an atomic type
CodePudding user response:
You don't even need enable_if
in this case, specialization is enough:
// By default, types are not atomic,
template<typename T> auto constexpr is_atomic = false;
// but std::atomic<T> types are,
template<typename T> auto constexpr is_atomic<std::atomic<T>> = true;
// as well as std::atomic_flag.
template<> auto constexpr is_atomic<std::atomic_flag> = true;
// Tests:
static_assert(!is_atomic<int>);
static_assert(is_atomic<std::atomic<int>>);
static_assert(is_atomic<std::atomic_flag>);
If you want to branch your compile-time logic based on this attribute, you can use C 17 if constexpr
:
template<typename T> auto foo(T& t) {
if constexpr (is_atomic<T>) /* T is atomic */;
else /* T is not atomic */;
}