c++ - Code with multiple inheritance and too much public access -
i have following chunk of code defining functor composition...
#pragma once #include <tuple> template<typename args,std::size_t a,typename...f> class _compose{}; template<typename...args,std::size_t a,typename f,typename...g> class _compose<std::tuple<args...>,a,f,g...> :private _compose<std::tuple<args...>,a-1,g...>{ const f f; public: _compose(f _f,g...g):_compose<std::tuple<args...>,a-1,g...>(g...),f(_f){}; constexpr auto operator()(args...args) const{ return f(_compose<std::tuple<args...>,a-1,g...>::operator()(args...)); } }; template<typename...args,typename f> class _compose<std::tuple<args...>,1,f>{ const f f; public: _compose(f _f):f(_f){}; constexpr auto operator()(args...args) const{ return f(args...); } }; template<typename...args> struct compose{ template<typename...fs> constexpr auto operator()(fs...f){ return _compose<std::tuple<args...>,sizeof...(fs),fs...>(f...); } };
using functor definitions such as:
template<typename t> struct func1{ constexpr auto operator()(t x){return x+1;} }; template<int i,typename t> class func2{ const int i,j; public: func2(int _j):i(i),j(_j){}; constexpr auto operator()(t x){return x+i+j;} };
i can compose compose<int>()(func1<int>(),func2<1,int>(1))(1)
return 4
.
now question: weak implementation? 1. went route of multiple inheritance each _compose<i>
inherits "downwards" _compose<i-1>
, on until _compose<1>
. felt elegant solution not sure of caveats. know multiple inheritance frowned upon. 2. unable make _compose
constructor more hidden outside world. tried make compose
function friend
none of attempts compiled. note _compose<...>::operator()
intended public compose
function returns _compose
object.
any suggestions improve above code appreciated. note compile under g++ -std=c++1y
.
thanks,
Comments
Post a Comment