HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Template with boost::operators extremely verbose and repeating

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
templateboostwithextremelyrepeatingverboseandoperators

Problem

I got the following ugly code:

template  class _Map = _Map >
class Polynomial
      : boost::ring_operators
      , boost::addable       , _Coeff
      , boost::addable       ,
                   std::pair, _Coeff >
      , boost::subtractable  , _Coeff
      , boost::subtractable  ,
                   std::pair, _Coeff >
      , boost::multipliable  , _Coeff
      , boost::multipliable  ,
                   std::pair,_Coeff >
                > > > > > > > {


In particular, I hate to repeat Polynomial. 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.

Solution

Easy, with default template arguments:

template  class _Map = _Map
       typename Poly = Polynomial,
       typename Pair = std::pair, _Coeff > >
class Polynomial
      : boost::ring_operators > > > > > > {

Code Snippets

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
                > > > > > > > {

Context

StackExchange Code Review Q#45345, answer score: 2

Revisions (0)

No revisions yet.