Last active
October 19, 2021 12:58
-
-
Save csghone/0b16834bb5d2e58f0dc6f87213c31c3d to your computer and use it in GitHub Desktop.
Example for enable_if_t
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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