4
\$\begingroup\$

I got the following ugly code:

template < class _Coeff,
       unsigned _nVars,
       typename _Expo=int,
       template <class, class> class _Map = _Map >
class Polynomial
      : boost::ring_operators< Polynomial<_Coeff,_nVars,_Expo,_Map>
      , boost::addable       < Polynomial<_Coeff,_nVars,_Expo,_Map>, _Coeff
      , boost::addable       < Polynomial<_Coeff,_nVars,_Expo,_Map>,
                   std::pair< std::array<_Expo,_nVars>, _Coeff >
      , boost::subtractable  < Polynomial<_Coeff,_nVars,_Expo,_Map>, _Coeff
      , boost::subtractable  < Polynomial<_Coeff,_nVars,_Expo,_Map>,
                   std::pair< std::array<_Expo,_nVars>, _Coeff >
      , boost::multipliable  < Polynomial<_Coeff,_nVars,_Expo,_Map>, _Coeff
      , boost::multipliable  < Polynomial<_Coeff,_nVars,_Expo,_Map>,
                   std::pair< std::array<_Expo,_nVars>,_Coeff >
                > > > > > > > {

In particular, I hate to repeat Polynomial<_Coeff,_nVars,_Expo,_Map>. Is there a way to improve this syntax without using the preprocessor ? I can't manage to get any help from the using keyword here.

Just for the infos: I'm writing multivariate polynomials where _Coeff is the class of the coefficients and the terms X1^4x2^4 are represented as std::array<_Expo,_nVars>.

\$\endgroup\$
2
\$\begingroup\$

Easy, with default template arguments:

template < class _Coeff,
       unsigned _nVars,
       typename _Expo=int,
       template <class, class> class _Map = _Map
       typename Poly = Polynomial<_Coeff,_nVars,_Expo,_Map>,
       typename Pair = std::pair< std::array<_Expo,_nVars>, _Coeff > >
class Polynomial
      : boost::ring_operators< Poly
      , boost::addable       < Poly, _Coeff
      , boost::addable       < Poly, Pair
      , boost::subtractable  < Poly, _Coeff
      , boost::subtractable  < Poly, Pair
      , boost::multipliable  < Poly, _Coeff
      , boost::multipliable  < Poly, Pair
                > > > > > > > {
|improve this answer|||||
\$\endgroup\$
  • 2
    \$\begingroup\$ I don't like it that much because it expose non-sensical parameter to the user. But then I can re-hide them by a front-end using template... OK ! \$\endgroup\$ – hivert Mar 26 '14 at 7:03
  • \$\begingroup\$ @hivert Indeed I, would also name this Polynomial_Impl or something, then add a template alias to this. The remaining default template arguments would go to the interface, not the implementation. \$\endgroup\$ – iavr Mar 26 '14 at 9:05
  • \$\begingroup\$ Add a std::enable_if at the end to check the user did not change those extra parameters \$\endgroup\$ – Manu343726 Jan 26 '15 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.