SFINAE alternative using Lambda functions
I don't know if it is a known hack. I found it by myself while working on a hobby project.
Below is a little example that returns a type based of a certain condition, for which usually template specialization is used.
struct Foo
{
Foo() = delete;
};
template <sizet I>
using type = decltype([]() -> auto {
if constexpr (I == 4)
{
return std::declval<int>();
}
else if constexpr (I == 6)
{
return std::declval<Foo>();
}
else
{
return std::declval<float>();
}
}());
staticassert(std::issamev<type<4>, int>);
staticassert(std::issamev<type<9>, float>);
staticassert(std::issamev<type<6>, Foo>);
https://redd.it/1q90r9g
@r_cpp
Post #24657
22