fork download
  1. template <class Derived>
  2. class base
  3. {
  4. public:
  5. constexpr auto operator()()
  6. const noexcept { return static_cast<const Derived&>(*this)(); }
  7. };
  8.  
  9. class derived : public base<derived>
  10. {
  11. public:
  12. constexpr auto operator()()
  13. const noexcept { return 0; }
  14. };
  15.  
  16. template <class A, class B>
  17. class expr : public base<expr<A, B>>
  18. {
  19. const A m_a;
  20. const B m_b;
  21. public:
  22. constexpr explicit expr(const A a, const B b)
  23. noexcept : m_a(a), m_b(b) {}
  24.  
  25. constexpr auto operator()()
  26. const noexcept { return m_a() + m_b(); }
  27. };
  28.  
  29. template <class D1, class D2>
  30. constexpr auto foo(const base<D1>& d1, const base<D2>& d2)
  31. noexcept { return expr<base<D1>, base<D2>>{d1, d2}; }
  32.  
  33. int main()
  34. {
  35. constexpr auto d = derived{};
  36. constexpr auto e = foo(d, d);
  37. static_assert(e() == 0, "");
  38. }
Success #stdin #stdout 0s 3092KB
stdin
Standard input is empty
stdout
Standard output is empty