How to conveniently handle long lists of parameters in C++ class templates? -


i ask guidelines on how ellegantly use long parameters lists in class templates. inconviency need repeat parameter list twice when defining (templated) methods, consts etc. in templated class.

the question similar this one, no example code provided , still fail compile of attempts. nothing gained when move parameters somewhere else have define long list of parameters in place(constructor, struct etc.).

i try give mwe. want achieve like:

#include <iostream>  template <class myint_t, int _n, int _m, myint_t _a, myint_t _b> class myclass{ // list longer , composed of fundamental classes // , myint_t template type-parameter   public:    static const int n = _n, m = _m; // way not keep different names                                     // params , fields? n = mypars.n?    // static const myint_t ma[2] = {_a, _b}; // ...but ofc not work    // cannot (or not know how to) pass arrays templates    static const myint_t ma[2];      // repetition of template parameters below    myint_t getsth();    // rest of implementation non-static fields , methods };  // static const array definition template <class myint_t, int _n, int _m, myint_t _a, myint_t _b>          const myint_t myclass<myint_t, _n, _m, _a, _b>::ma[2] = {_a,_b};  // , defining method... template <class myint_t, int _n, int _m, myint_t _a, myint_t _b>   myint_t myclass<myint_t, _n, _m, _a, _b>::getsth(){     std::cout << "yey! " << n << ", " << m << " || " << ma[1] << std::endl;     return ma[0];   }  typedef myclass<long long unsigned, 5,6, ~123ull, -5ull> myspecclass1; typedef myclass<long unsigned, 13,19, 123ull, 456ull>    myspecclass2;  // main() int main() {   myspecclass1 obj1;   long long unsigned test = obj1.getsth();   std::cout << "bye! " << test << " || " << myspecclass2::n << std::endl;   return 0; } 

output:

yey! 5, 6 || 18446744073709551611 bye! 18446744073709551492 || 13 

note1: i'm shy use preprocessor definitions, when list of parameteres has more ten constant parameters notoriously have repeat becomes imho not readable. whereas purpose of templates clean way utilize code many times... must doing sth wrong... there problem of having 2 different names 1 parameter: names of field , template non-type constant have different.

note2: want static constants because characterise whole specialised class , not instance of it. const because of compiler optimisation.

summary: feel code above not ellegant , has tendency grow lot uglier when list of constants elongating. there lot of mathematical algorythms (like random number generators, solvers) take long list of static constants.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -