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\$

1 Answer 1

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
                > > > > > > > {
\$\endgroup\$
3
  • 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
    Commented Mar 26, 2014 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
    Commented Mar 26, 2014 at 9:05
  • \$\begingroup\$ Add a std::enable_if at the end to check the user did not change those extra parameters \$\endgroup\$
    – Manu343726
    Commented Jan 26, 2015 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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