How to negate std::enable_if

Also see std::enable_if minimal example and std::enable_if and std::is_floating_point minimal examp le

Since the template argument to std::enable_if is a boolean, you can easily negate it using the ! operator.

Example:

// This function uses normal (non-negated) std::enable_if:
template<typename T, typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr> 
T mySineFloatingPointOnly(T arg) {
    return sin(arg);
}

// This function has negated std::enable_if:
template<typename T, typename std::enable_if<!std::is_floating_point<T>::value>::type* = nullptr> 
T mySineNOFloatingPoint(T arg) {
    return sin(arg);
}

Full example:

#include <iostream>
#include <type_traits>
#include <cmath>

using std::cout;
using std::endl;

template<typename T>
T mySine(T arg) {
    return sin(arg);
}

template<typename T, typename std::enable_if<std::is_floating_point<T>::value>::type* = nullptr> 
T mySineFloatingPointOnly(T arg) {
    return sin(arg);
}

template<typename T, typename std::enable_if<!std::is_floating_point<T>::value>::type* = nullptr> 
T mySineNOFloatingPoint(T arg) {
    return sin(arg);
}

int main() {
    cout << mySine(1.5) << endl;
    // mySine(1) will work
    // mySineFloatingPointOnly(1) will fail to compile
    // mySineNOFloatingPoint(1.5) will fail to compile
    cout << mySineFloatingPointOnly(1.5) << endl;
    cout << mySineNOFloatingPoint(1) << endl;
}