Request Short Link
C++ 98
C++ 11
C++ 14
C++ 17
C++ 20
C++ 23
C++ 2c
for-loops as while-loops
array subscription
Show all implicit casts
Show all template parameters of a CallExpr
Use libc++
Transform std::initializer_list
Show noexcept internals
Show padding information
Show coroutine transformation
Show C++ to C transformation
Show object lifetime
Default
15
18
20
22
26
More
GitHub
Patreon
Issues
About
Policies
Examples
C++ Insights @ YouTube
Settings
Version
New C++ Insights Episode
×
Made by
Andreas Fertig
Powered by
Flask
and
CodeMirror
Source:
#include <utility> template<class L> struct Leaf { template<class M> static L test(const M&); // match L's without Def template<class M> // match L's with Def static auto test(M&&) -> typename Leaf<typename M::Def>::type; // find matching test() overload, prefer test(M&&): using type = decltype( test(std::declval<L>()) ); }; template<class L> // helper types using Leaf_t = typename Leaf<L>::type; //---------------------------------------------------------------------- #include <iostream> #include <type_traits> template<class D> class HasDef { public: using Def = D; }; class A : public HasDef<class B> {}; class B : public HasDef<class C> {}; class C {}; int main() { Leaf_t<A> la; // has type C Leaf_t<B> lb; // has type C Leaf_t<C> lc; // has type C static_assert(std::is_same<decltype(la), C>::value, ""); static_assert(std::is_same<decltype(lb), C>::value, ""); static_assert(std::is_same<decltype(lc), C>::value, ""); }
Insight:
Console: