TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24657 22
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>();
}
}());

static
assert(std::issamev<type<4>, int>);

staticassert(std::issamev<type<9>, float>);

static
assert(std::issamev<type<6>, Foo>);

https://redd.it/1q90r9g
@r_cpp
Reddit SFINAE alternative using Lambda functions : r/cpp 52 votes, 37 comments. 355K subscribers in the cpp community. Discussions, articles and news about the C++ programming language or programming in C++.
More from @r_cpp
  1. Oct 8, 2026Profiles for simplicity and guarantees - Bjarne Stroustrup - CppCon 2026 https://youtu.be/…
  2. Oct 8, 2026How to Fix autoconf-style Configuration Probing https://build2.org/blog/fix-autoconf.xhtml…
  3. Oct 8, 2026owl – a header-only C++23 HTTP framework where route patterns and handler extractors are c…
  4. Oct 8, 2026Removed by Reddit Removed by Reddit on account of violating the [content policy. ] https:/…
  5. Oct 8, 2026Is there a way to search across all P and N papers throughout standardization history? It…
  6. Oct 8, 2026CppCast Episode 412 : Safety and Interoperability with Rust and C++ https://cppcast.com/sa…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →