传递给<N>回调函数的模板化迭代器



我正在尝试运行一个编译时迭代器:

meta::reverse_iterator<2, 9>::iterate(callback());
meta::reverse_iterator<4, 7>::iterate(callback());
std::cout << "-----------------" << std::endl;
meta::iterator<2, 9>::iterate(callback());
meta::iterator<4, 7>::iterate(callback());
struct callback {
  template <int i>
  void operator()() {
    std::cout << "print !!" << i << std::endl;
  }
};

这就是我写了元文字的方式:

namespace meta {
    template <int Begin, int End, bool done = false>
    struct reverse_iterator {
        template <typename F>
        static void iterate(F f) {
            f.template operator()<End>();
            reverse_iterator<Begin, End-1, Begin == End-1>::iterate(f);
        }
    };
    template <int Begin, int End>
    struct reverse_iterator<Begin, End, true> {
        template <typename F>
        static void iterate(F) {}
    };
    template <int Begin, int End, bool done = false>
    struct iterator {
        template <typename F>
        static void iterate(F f) {
            iterator<Begin, End - 1, Begin == End - 1>::iterate(f);
            f.template operator()<End - 1>();
        }
    };
    template <int Begin, int End>
    struct iterator<Begin, End, true> {
        template <typename F>
        static void iterate(F) {}
    };
}

现在,我迭代了operator()<N>,但我希望它能够通过模板参数<N>(不是运行时参数)来调用用户提供的任何任意功能?

如何实现?

boost::bind也无法使用它,因为它调用绑定的函数对象而不是真实函数。因此,应该有某种方法将默认参数运送到提供的功能。

afaik,您不能直接执行此操作,但是如果您真的想要它,则可以使用traits类为您呼叫成员函数,而对于不同的成员函数,请写不同的特征:

struct function_operator_call_trait {
    template< int N, class T >
    void call( T& t ) {t.template operator()<N>();}
};
template< class Arg1, class Arg2 >
struct foo_call_trait {
    foo_call_trait( Arg1&& arg1 ) : a1( std::move(arg1) ) {}
    foo_call_trait( Arg1&& arg1, Arg2&& arg2 )
        : a1( std::move(arg1) ), a2( std::move(arg2) ) {}
    template< class T, int N >
    void call( T& t ) {t.template foo<N>(a1, a2);}
    Arg1 a1;
    Arg2 a2;
};
template <int Begin, int End, class traits = function_operator_call_trait, bool done = false>
struct iterator{
    iterator() {}
    iterator( traits const& t ) : t_( t ) {}
    iterator( traits&& t ) : t_( std::move(t) ) {}
    template<typename F>
    static void iterate(F f){
        iterator<Begin, End-1, traits, Begin == End-1>::iterate(f);
        t_.call<End-1>( f );
    }
    traits t_;
};
typedef iterator<2, 9> fc_iterator;
typedef foo_call_trait<int, float> foo_traits;
typedef iterator<2, 9, foo_traits> foo_iterator( foo_traits(1, 2) );

最新更新