Created
August 17, 2022 22:29
-
-
Save DarkStar1997/43b687e3c054d962f197d6e37412a7be to your computer and use it in GitHub Desktop.
Sample SFINAE examples
This file contains 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> | |
#include <type_traits> | |
struct tag1{}; | |
struct tag2{}; | |
enum class Tags { | |
type1, | |
type2, | |
}; | |
//template <typename T, std::enable_if<(T::type == Tags::type1), void>> | |
template <typename T, std::enable_if_t<(T::type == Tags::type1)> * = nullptr> | |
void lol(T obj) | |
{ | |
std::cout << "lol " << (int)T::type << '\n'; | |
} | |
template <typename T, std::enable_if_t<(T::type == Tags::type2)> * = nullptr> | |
void lol(T obj) | |
{ | |
std::cout << "lol " << (int)T::type << '\n'; | |
} | |
template <typename T, std::enable_if_t<std::is_same<typename T::tag, tag1>::value, bool> = true> | |
void display(T obj) | |
{ | |
std::cout << "tag1 display\n"; | |
} | |
template <typename T, std::enable_if_t<std::is_same<typename T::tag, tag2>::value, bool> = true> | |
void display(T obj) | |
{ | |
std::cout << "tag2 display\n"; | |
} | |
struct Data1 {using tag = tag1; }; | |
struct Data2 {using tag = tag2; }; | |
struct LolData1 {static constexpr Tags type = Tags::type1; }; | |
struct LolData2 {static constexpr Tags type = Tags::type2; }; | |
int main() | |
{ | |
Data1 obj1; | |
Data2 obj2; | |
display(obj1); | |
display(obj2); | |
LolData1 data1; | |
lol(data1); | |
LolData2 data2; | |
lol(data2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment