How do I get a template parameter pack to infer pass-by-reference rather than pass-by-value?

后端 未结 1 1746
北荒
北荒 2021-01-24 23:06

In the following class, wrapper takes a pointer to an arbitrary const method and returns the result of a call to that method with const re

相关标签:
1条回答
  • 2021-01-24 23:37

    You can perfect forward the arguments to the function:

    template<typename T, typename... Ts, typename... Args>
    auto& wrapper(T const& (C::*f)(Ts...) const, Args&&... args) {
      return const_cast<T&>((this->*f)(std::forward<Args>(args)...));
    }
    

    This is achieved by making args a forwarding reference parameter pack. Note that we need to introduce a new Args template parameter pack in order to deduce the arguments correctly.

    0 讨论(0)
提交回复
热议问题