c++ - Template parameters simplification -


i new c++ (using c++ 2011) , find solution following problem. have class represents fonction:

class curve { private:     ... public:     std::array<double, 3> value(double s); } 

i using object pass function algorithm represented class:

template <int n, typename functor> class algorithm { private:     functor f;     std::array<double, n> a; public:     ... } 

then create object

algorithm<3, curve> solver; 

but 3 3 coming size of array returned method value of objet of type curve. simplify code can use :

algorithm<curve> solver; 

but have no idea on how that. mind giving me hint ?

best regards, francois

add member array_length or similar curve classes:

class curve { public:     static constexpr const std::size_t length = 3;  private:     std::array<double,length> a; };  template<typename algorithm , std::size_t n = algorithm::length> class algorithm {     ... }; 

if need allow classic function entities algorithms, approach doesn't work since there no length member. other way create metafunction data_length given algorithm function f returns length of data:

template<typename f> struct length;  //specialization curve class: template<> struct length<curve> : public std::integral_constant<std::size_t,3> {}; 

and then:

template<typename f , std::size_t n = length<f>::value> struct algorithm {    ... }; 

edit: in other template, implement method specify template parameters:

template<typename f , std::size_t n> void algorithm<f,n>::execute() {     _f(); } 

here running example.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -