Guest User

Untitled

a guest
May 31st, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. old_operation(stream, format, varArgs);
  2.  
  3. stream << operation(format, varArgs);
  4.  
  5. template<typename ...T>
  6. decltype(auto) storage(T&& ...t) {
  7. return [&](auto&& f) ->decltype(auto) {
  8. return std::forward<decltype(f)>(f)(t...);
  9. };
  10. }
  11.  
  12. template<typename ...T>
  13. class Operation
  14. {
  15. using Storage = decltype(storage(std::declval<T>()...));
  16. public:
  17. template<typename ...Args>
  18. explicit Operation(Args&& ...args) :
  19. mArgs(storage(std::forward<Args>(args)...)) {};
  20. template<class StreamType>
  21. StreamType& Apply(StreamType& stream)
  22. {
  23. auto f = [&](auto&& ...xs)
  24. {
  25. old_operation(stream, std::forward<decltype(xs)>(xs)...);
  26. }
  27. mArgs(f);
  28. return stream;
  29. }
  30. private:
  31. Storage mArgs;
  32. };
  33.  
  34. template<typename ...Args>
  35. Operation<Args...> MakeOperation(Args&&... args)
  36. {
  37. return Operation<Args...>(std::forward<Args>(args)...);
  38. }
  39.  
  40. template<class StreamType, typename ...Args>
  41. StreamType& operator<<(StreamType& stream, Operation<Args...>&& operation)
  42. {
  43. return operation.Apply(stream);
  44. }
  45.  
  46. namespace X {namespace Y { namespace Z { int formater(double x) { return std::round(x); }}}
  47.  
  48. #define OPERATION(...)
  49. [&]() {
  50. using namespace ::X:Y:Z;
  51. return Operation("" __VA_ARGS__); }()
  52.  
  53. stream << OPERATION(format, formater(2.3));
Add Comment
Please, Sign In to add comment