Created
January 21, 2023 16:30
-
-
Save robertshepherdcpp/36a85ab6969f7231c38de23a180db5a6 to your computer and use it in GitHub Desktop.
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> | |
template<auto A, auto B, auto first_, auto second_, typename to_compare> | |
struct inline_if | |
{ | |
constexpr auto operator()() | |
{ | |
if constexpr(to_compare{}.template operation<A, B>()) | |
{ | |
return first_; | |
} | |
else | |
{ | |
return second_; | |
} | |
} | |
}; | |
struct is_same_value | |
{ | |
template<auto Az, auto Bz> | |
constexpr auto operation() | |
{ | |
return Az == Bz; | |
} | |
}; | |
template<auto X> | |
struct foo | |
{ | |
static constexpr auto value = inline_if<X, 42, 'c', true, is_same_value>{}.operator()(); | |
}; | |
int main() | |
{ | |
std::cout << foo<42>::value; // outputs 'c' | |
std::cout << foo<'x'>::value; // outputs 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment