开发者

Recursively unhide base class members

开发者 https://www.devze.com 2023-03-14 06:40 出处:网络
I try to write a class that takes a tuple of functions as its argument and overloads operator() for all argument_types of the function. Right now this looks like this:

I try to write a class that takes a tuple of functions as its argument and overloads operator() for all argument_types of the function. Right now this looks like this:

template<typename T>
struct holder {
  T t;
};

template<typename T, std::size_t i>
struct overload_helper : public holder<T>, public overload_helper<T, i - 1> {
  overload_helper(T t) : holder<T>({t}) {};
  typedef typename std::tuple_element<i - 1, T>::type inner_type;
  typename inner_type::result_type operator()(typename inner_type::argument_type x) { 
    return std::get<i - 1>(holder<T>::t)(x); }
};

template<typename T>
struct overload_helper<T, 1> {
  typedef typename std::tuple_element<0 ,T>::type inner_type;
  typename inner_type::result_type operator()(typename inner_type::argument_type x) { 
    return std::get<0>(holder<T>::t)(x); }
};

template<typename T>
struct overload : public overload_helper<T, std::tuple_size<T>::value>
{
  typedef void result_type;
  overload(const T& t) : overload_helper<T, std::tuple_size<T>::value>(t) { }
};


int main() {
  auto all = make_overload(std::make_tuple(
                 std::function<void(double)>([](double i) { 
                 std::cout << "double" <&开发者_StackOverflowlt; std::endl;
                   }), std::function<void(int)>([](int i) { 
                   std::cout << "int" << std::endl; })));

  all(1); //fails
  all(1.0);
}

The problem is that the base class is hiding each recursive definition of operator(). Is it possible to recursively unhide all definitions with using or is the only way to have a templated operator() and pick the right overload with boost::mpl?


using overload_helper<T, i - 1>::operator() in each overload_helper should do the job, so long they aren't ambiguous.

0

精彩评论

暂无评论...
验证码 换一张
取 消