perfect_fwd.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Copyright 2006-2014 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/flyweight for library home page.
  7. */
  8. #ifndef BOOST_FLYWEIGHT_DETAIL_PERFECT_FWD_HPP
  9. #define BOOST_FLYWEIGHT_DETAIL_PERFECT_FWD_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. /* C++03-compatible implementation of perfect forwarding.
  14. * Usage:
  15. *
  16. * # define NAME ...
  17. * # define BODY(args) {...BOOST_FLYWEIGHT_FORWARD(args)...}
  18. * BOOST_FLYWEIGHT_PERFECT_FWD(name,body)
  19. *
  20. * where NAME includes the return type and qualifiers (if any) and BODY(args)
  21. * is expected to fo the forwarding through BOOST_FLYWEIGHT_FORWARD(args).
  22. *
  23. * In compilers capable of perfect forwarding, the real thing is provided
  24. * (just one variadic args overload is generated). Otherwise the machinery
  25. * generates n+1 overloads, if rvalue refs are supported, or else 2^(n+1)-1
  26. * overloads accepting any combination of lvalue refs and const lvalue refs,
  27. * up to BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS args.
  28. *
  29. * BOOST_FLYWEIGHT_PERFECT_FWD_WITH_ARGS(name,body) is a variation omitting the
  30. * overloads with zero args --when perfect forwarding is available, this second
  31. * macro is exactly the same as the original.
  32. */
  33. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  34. #include <boost/preprocessor/cat.hpp>
  35. #include <boost/preprocessor/repetition/enum.hpp>
  36. #include <boost/preprocessor/repetition/enum_params.hpp>
  37. #include <boost/preprocessor/seq/seq.hpp>
  38. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  39. #include <utility>
  40. #endif
  41. #define BOOST_FLYWEIGHT_FORWARD_FORWARD_AUX(z,n,_) \
  42. std::forward<BOOST_PP_CAT(T,n)>(BOOST_PP_CAT(t,n))
  43. #define BOOST_FLYWEIGHT_FORWARD_FORWARD(n) \
  44. BOOST_PP_ENUM(n,BOOST_FLYWEIGHT_FORWARD_FORWARD_AUX,~)
  45. #define BOOST_FLYWEIGHT_FORWARD_ENUM(n) BOOST_PP_ENUM_PARAMS(n,t)
  46. #define BOOST_FLYWEIGHT_FORWARD_PASS(arg) arg
  47. #define BOOST_FLYWEIGHT_FORWARD(args)\
  48. BOOST_PP_CAT(BOOST_FLYWEIGHT_FORWARD_,BOOST_PP_SEQ_HEAD(args))( \
  49. BOOST_PP_SEQ_HEAD(BOOST_PP_SEQ_TAIL(args)))
  50. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)||\
  51. defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  52. #if !defined(BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS)
  53. #define BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS 5
  54. #endif
  55. #if BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS<0
  56. #error BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS must be >=0
  57. #endif
  58. #if BOOST_FLYWEIGHT_LIMIT_PERFECT_FWD_ARGS<=5
  59. #include <boost/flyweight/detail/pp_perfect_fwd.hpp>
  60. #else
  61. #include <boost/flyweight/detail/dyn_perfect_fwd.hpp>
  62. #endif
  63. #else
  64. /* real perfect forwarding */
  65. #define BOOST_FLYWEIGHT_PERFECT_FWD(name,body) \
  66. template<typename... Args>name(Args&&... args) \
  67. body((PASS)(std::forward<Args>(args)...))
  68. #define BOOST_FLYWEIGHT_PERFECT_FWD_WITH_ARGS \
  69. BOOST_FLYWEIGHT_PERFECT_FWD
  70. #endif
  71. #endif