Last active
December 20, 2022 04:39
-
-
Save clucle/40fb189382473129809c5afb9682d339 to your computer and use it in GitHub Desktop.
how enable if use
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
#include <iostream> | |
class A | |
{ | |
public: | |
static constexpr bool dujin = true; | |
}; | |
class B | |
{ | |
public: | |
static constexpr bool dujin = false; | |
}; | |
class EnableIfTester | |
{ | |
public: | |
template<class T> | |
typename std::enable_if<T::dujin, bool>::type check() | |
{ | |
return true; | |
} | |
template<class T> | |
typename std::enable_if<!T::dujin, bool>::type check() | |
{ | |
return false; | |
} | |
template < | |
typename T, | |
std::enable_if_t<T::dujin, bool> = true > | |
void check2() | |
{ | |
std::cout << "true\n"; | |
} | |
template < | |
typename T, | |
std::enable_if_t<!T::dujin, bool> = true > | |
void check2() | |
{ | |
std::cout << "false\n"; | |
} | |
template< | |
typename T > | |
void check3() | |
{ | |
if constexpr ( T::dujin ) | |
std::cout << "true\n"; | |
else | |
std::cout << "false\n"; | |
} | |
}; | |
int main() | |
{ | |
EnableIfTester tester; | |
std::cout << tester.check<A>() << '\n'; | |
std::cout << tester.check<B>() << '\n'; | |
tester.check2<A>(); | |
tester.check2<B>(); | |
tester.check3<A>(); | |
tester.check3<B>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment