Skip to content

Instantly share code, notes, and snippets.

@csghone
Last active October 19, 2021 12:58
Show Gist options
  • Select an option

  • Save csghone/0b16834bb5d2e58f0dc6f87213c31c3d to your computer and use it in GitHub Desktop.

Select an option

Save csghone/0b16834bb5d2e58f0dc6f87213c31c3d to your computer and use it in GitHub Desktop.
Example for enable_if_t
// Ref: https://en.cppreference.com/w/cpp/types/enable_if
#include <type_traits>
#include <iostream>
struct TypeTrue {
static constexpr bool enable_if_condition = true;
};
struct TypeFalse {
static constexpr bool enable_if_condition = false;
};
template <typename T>
class MyClass {
public:
template <typename C = T>
typename std::enable_if_t<!C::enable_if_condition, void>
test_fxn() {
if (T::enable_if_condition) {
std::cout << "AAAA" << std::endl;
}
std::cout << "BBBB" << std::endl;
}
template <typename C = T>
typename std::enable_if_t<C::enable_if_condition, void>
test_fxn() {
if (T::enable_if_condition) {
std::cout << "CCCC" << std::endl;
}
std::cout << "DDDD" << std::endl;
}
};
int main() {
MyClass<TypeTrue> a;
MyClass<TypeFalse> b;
a.test_fxn();
b.test_fxn();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment