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>
.
1 Answer 1
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
> > > > > > > {
-
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– hivert2014年03月26日 07:03:37 +00:00Commented 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– iavr2014年03月26日 09:05:08 +00:00Commented 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– Manu3437262015年01月26日 17:40:52 +00:00Commented Jan 26, 2015 at 17:40