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

后端 未结 1 1759
北荒
北荒 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
    auto& wrapper(T const& (C::*f)(Ts...) const, Args&&... args) {
      return const_cast((this->*f)(std::forward(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)
提交回复
热议问题