fork download
  1. // User-defined infix operator framework
  2.  
  3. template <typename LeftOperand, typename Operation>
  4. struct LeftHelper
  5. {
  6. const LeftOperand& leftOperand;
  7. const Operation& operation;
  8. LeftHelper(const LeftOperand& leftOperand, const Operation& operation)
  9. : leftOperand(leftOperand), operation(operation) {}
  10. };
  11.  
  12. template <typename LeftOperand, typename Operation >
  13. auto operator < (const LeftOperand& leftOperand, Operation& operation)
  14. {
  15. return LeftHelper<LeftOperand, Operation>(leftOperand, operation);
  16. }
  17.  
  18. template <typename LeftOperand, typename Operation, typename RightOperand>
  19. auto operator > (LeftHelper<LeftOperand, Operation> leftHelper, const RightOperand& rightOperand)
  20. {
  21. return leftHelper.operation(leftHelper.leftOperand, rightOperand);
  22. }
  23.  
  24. // Defining a new operator
  25.  
  26. #include <cmath>
  27. static auto pwr = [](const auto& operand1, const auto& operand2) { return std::pow(operand1, operand2); };
  28.  
  29. // using it
  30. #include <iostream>
  31. int main()
  32. {
  33. std::cout << (2 <pwr> 16) << std::endl;
  34. return 0;
  35. }
  36.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
65536