Originally Posted by
MutantJohn
There's no longer any code that I cannot eventually understand now!
You will eventually understand this
But I am hoping to waste a lot of your time.
What is happening here is easy to see, but the way it is achieved is ridiculously complicated!
If you have an explanation immediately, kudos wizard, you win!
Code:
#include <iostream>
template< typename T, T v >
struct Value {
static constexpr T value = v;
};
template< typename , typename = void >
struct Bar : Value<bool, false> {};
template< typename ... > using Foo = void;
template< typename T >
struct Bar< T ,
Foo < decltype(std::declval<T>().foo()) >>
: Value<bool, true> {
};
template<bool B, typename T = void>
struct Cond {};
template<typename T>
struct Cond<true, T> { typedef T TT; };
template<typename Direction>
struct CallFoo {
template< typename U = int >
typename Cond< Bar<Direction>::value, U >::TT foo() {
return Direction().foo();
}
template< typename U = int >
typename Cond< !Bar<Direction>::value, U >::TT foo() {
return 42;
}
};
struct Minus42{
int foo() {return -42;};
};
int main() {
CallFoo<Minus42> left;
CallFoo<Value<int, 42>> notLeft;
std::cout << left.foo() + notLeft.foo();
return 0;
}