C++: 2 templates vs. 1 template specialization on complex class -


i have custom rather complex data structure form:

class root;  class tree {  public:     ... // lots of members here, child access etc     exactbar *bar() { return mbar; }     exactqux *qux() { return mqux; }  private:     ... // lots of members here     exactbar *mbar;     exactqux *mqux; };  class root : public tree { // root manages memory tree nodes!     ... private:     memoryspace<tree> mnodes;     memoryspace<exactbar> mbars;     memoryspace<exactbar> mquxs; }; 

the purpose of program build tree requires exact types above. requires massive amounts of memory , i'm literally stretched 32-bit limit. want to, once tree built, convert whole tree inexact type not faster, takes less memory.

but of course keep functionality tree offers methods, , of course other algorithms work on tree. writing new class out of question. templates seem appropriate here. however, i'm wondering 2 ways of writing new, templated class. 1 obvious way is

template <typename bar, typename qux> class tree {  public:     ... // lots of members here, child access etc     bar *bar() { return mbar; }     qux *qux() { return mqux; }  private:     ... // lots of members here     bar *mbar;     qux *mqux; };  template <typename bar, typename qux> class root : public tree<bar, qux> {     ... private:     memoryspace<tree> mnodes;     memoryspace<bar> mbars;     memoryspace<qux> mquxs; }; 

however definition seems bit awkward. mean, know never use tree<inexactbar, exactqux> instance. bad practice? sure, can solve long name typedef still. solution thought this:

template <typename exactness> class tree;  template <> class tree<exact> {  public:     ... // lots of members here, child access etc     exactbar *bar() { return mbar; }     exactqux *qux() { return mqux; }  private:     ... // lots of members here     exactbar *mbar;     exactqux *mqux; }; // ... similar inexact 

which method preferable, there patterns or anti-patterns or ripple effects doing should aware of? thanks!!

here's option, minimizes number of template parameters , amount of duplicated code:

struct exact {     typedef exactbar bartype;     typedef exactqux quxtype; };  struct inexact {     typedef inexactbar bartype;     typedef inexactqux quxtype; };  template <typename exactness> class tree {  public:     typedef typename exactness::bartype bar;     typedef typename exactness::quxtype qux;      ... // lots of members here, child access etc     bar *bar() { return mbar; }     qux *qux() { return mqux; }  private:     ... // lots of members here     bar *mbar;     qux *mqux; }; 

this similar policy-based design. of course still have templatize other functions deal tree type, there's still ripple effect. although partially dodge ripple effect so:

template <typename exactness> class ttree {     ... // lots of members };  typedef ttree<exact> tree; // same name before! typedef ttree<inexact> inexacttree; 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -