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

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Mar 25, 2014 at 21:03
\$\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
 > > > > > > > {
answered Mar 25, 2014 at 23:03
\$\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\$ 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\$ 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\$ Commented Jan 26, 2015 at 17:40

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.